0
目前,我有以下各表的东西:实体framwork - 左加入或simalar
AspNetUsers(Id, PasswordHash,....) //standard ASP Identity 2.0 table
Category(Id, Name)
CategoryUser(CategoryId, UserId) // link table to turn many-to-many into a one-to-many
我的班匹配这些实体:
[Table("Category")]
public class Category
{
[Key]
public Int32? CategoryId { get; set; }
public string CategoryName { get; set; }
public Int32? Category_ParentID { get; set; }
public virtual Category Parent_Category { get; set; }
public virtual ICollection<UserCategory> Parent_UserCategories { get; set; }
public virtual ICollection<Category> SubCategories { get; set; }
public Int16 CategoryOrder { get; set; }
public bool Deleted { get; set; }
}
[Table("UserCategory")]
public class UserCategory
{
[Key]
[Column(Order=1)]
public Int32 UserId { get; set; }
[Key]
[Column(Order = 2)]
public Int32 CategoryId { get; set; }
public virtual Category UsersCategory { get; set; }
public virtual ApplicationUser UserCategoryUser { get; set; }
public bool CanEditCategory { get; set; }
public bool CanDeleteCategory { get; set; }
public bool CanViewCategory { get; set; }
public bool CanChangePermissions { get; set; }
}
该查询返回被发送到视图模型:
//Retrive the category-if the user has access
Password selectedCategory = DatabaseContext.Category.Include("Parent_UserCategories").Where(pass => !pass.Deleted
&& (
(DatabaseContext.UserCategories.Any(up => up.CategoryId == pass.CategoryId && up.UserId == UserId))
|| (userIsAdmin && ApplicationSettings.Default.AdminsHaveAccessToAllPasswords)
|| pass.Creator_Id == UserId)
).SingleOrDefault(p => p.CategoryId == 1);
我想要做的是有一个视图,它将显示所有的用户和他们拥有的访问权限。我已经有回发的视图设置...但它只显示数据库中已经存在的权限记录。我需要列出所有用户(无论他们是否有CategoryUser记录)以及相应的CategoryUser记录(如果他们有记录)。
我有很多想法可以使这项工作,但理想情况下,我想最有效和最干净的解决方案 - 我也试图避免LINQ 2 SQL。