C# ASP.NET查询泛型DBSet<;T>;

C# ASP.NET查询泛型DBSet<;T>;,c#,asp.net-mvc,linq,entity-framework,generics,C#,Asp.net Mvc,Linq,Entity Framework,Generics,我正在尝试一个场景,在这个场景中,我可以传入一个实体模型,并检查它是否有UserFK,如果有,并且当前用户不在admin角色中。。检查UserFK是否与数据库中当前用户的UserId匹配 我就是搞不懂泛型的最后一点。。我想我的思路是对的,但不太确定 [AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)] public class IsOwnerAttribute<T> : A

我正在尝试一个场景,在这个场景中,我可以传入一个实体模型,并检查它是否有UserFK,如果有,并且当前用户不在admin角色中。。检查UserFK是否与数据库中当前用户的UserId匹配

我就是搞不懂泛型的最后一点。。我想我的思路是对的,但不太确定

[AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public class IsOwnerAttribute<T> : AuthorizeAttribute where T : class
{
    public IsOwnerAttribute(IUnitOfWork context)
    {
        this.context = context;
    }

    public string RouteParameter
    {
        get { return this.routeParameter; }
        set { this.routeParameter = value; }
    }

    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        if (filterContext == null)
        {
            throw new ArgumentNullException("filterContext");
        }

        if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
        {
            filterContext.Result = new HttpUnauthorizedResult();
        }
        else if (IsOwner(filterContext))
        {
            return;
        }
        else
        {
            ViewDataDictionary viewData = new ViewDataDictionary();
            viewData.Add("Message", "You do not have sufficient privileges for this operation.");
            filterContext.Result = new ViewResult { ViewName = "Error", ViewData = viewData };
        }

    }

    bool IsOwner(AuthorizationContext filterContext)
    {
        bool result = false;

        int id = -1;
        if (filterContext.RouteData.Values.ContainsKey(this.RouteParameter))
        {
            id = Convert.ToInt32(filterContext.RouteData.Values[this.RouteParameter]);
        }

        var currentUser = Membership.GetUser();
        if (currentUser != null && !filterContext.HttpContext.User.IsInRole("Administrator"))
        {
            var userGuid = (Guid)currentUser.ProviderUserKey;

            // Stuck here.. trying to work out how with the Set<T> how i could then check if it has an Id property and a UserFK property and if it does then basically look up if the ID matches the ID in the route and the UserFK matches the userGuid then let them access the content...
            result = context.Set<T>().Where(c => c.Id == id && c.UserFK == userGuid).SingleOrDefault() != null;

        }

        return result;
    }

    string routeParameter = "id";
    readonly IUnitOfWork context;
    readonly IDbSet<T> dbset;
}
[AttributeUsage(AttributeTargets.Method,Inherited=true,AllowMultiple=false)]
公共类IsOwnerAttribute:AuthorizeAttribute,其中T:class
{
公共IsOwnerAttribute(IUnitOfWork上下文)
{
this.context=上下文;
}
公共字符串路由参数
{
获取{返回this.routeParameter;}
设置{this.routeParameter=value;}
}
授权时的公共覆盖无效(AuthorizationContext filterContext)
{
如果(filterContext==null)
{
抛出新ArgumentNullException(“filterContext”);
}
如果(!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
filterContext.Result=新的HttpUnauthorizedResult();
}
else if(IsOwner(filterContext))
{
返回;
}
其他的
{
ViewDataDictionary viewData=新建ViewDataDictionary();
添加(“消息”,“您没有足够的权限执行此操作”);
filterContext.Result=newviewResult{ViewName=“Error”,ViewData=ViewData};
}
}
bool IsOwner(授权上下文筛选器上下文)
{
布尔结果=假;
int id=-1;
if(filterContext.RouteData.Values.ContainsKey(this.RouteParameter))
{
id=Convert.ToInt32(filterContext.RouteData.Values[this.RouteParameter]);
}
var currentUser=Membership.GetUser();
if(currentUser!=null&&!filterContext.HttpContext.User.IsInRole(“管理员”))
{
var userGuid=(Guid)currentUser.ProviderUserKey;
//卡在这里..尝试解决如何使用Set,然后检查它是否有Id属性和UserFK属性,如果有,然后基本上查找Id是否与路由中的Id匹配,UserFK是否与userGuid匹配,然后让他们访问内容。。。
result=context.Set()。其中(c=>c.Id==Id&&c.UserFK==userGuid)。SingleOrDefault()!=null;
}
返回结果;
}
字符串routeParameter=“id”;
只读IUnitOfWork上下文;
只读IDbSet数据库集;
}

我不知道我是否有错误的想法,或者是否有更好的方法,但我想知道可能的情况。

为什么使用Where Where可以使用Find Where Find将搜索t主键,并使用object作为参数。我想这会解决你的问题

示例更改如下:

result = context.Set<T>().Where(c => c.Id == id && c.UserFK == userGuid).SingleOrDefault() != null;
result=context.Set()。其中(c=>c.Id==Id&&c.UserFK==userGuid)。SingleOrDefault()!=无效的
用这个

result = context.Set<T>().Find(id);//and you don't need to filter also with user if your ID is primary key of the table 
result=context.Set().Find(id)//而且,如果您的ID是表的主键,则不需要对用户进行筛选

您的属性做得太多了。防止在属性中实现任何逻辑(尤其是使您与数据库通信的东西)。此外,属性不能是泛型类型。