2015-09-21 25 views
1

我有以下表格:User,UserGroups和UserInGroups。你可以在下面的圖片上看到它們。當我打電話用戶時,我希望能夠獲取用戶所在的組(UserInGroups)。我正在閱讀有關EntityFramework的資料,但我無法在代碼中建立連接,因此我錯過了什麼?我需要在模型創建上連接它們嗎?EntityFramework代碼第一鍵

目前我沒有從UserInGroup或UserGroups獲取任何數據。

DB看起來像這樣 enter image description here

我的課是這樣的:

public class User : BaseEntity 
{ 
    public int RoleId { get; set; } 

    public Role Role { get; set; } 

    public UserGroup UserGroup { get; set; } 

    public UserInGroup UserInGroup { get; set; } 

} 

public class UserGroup : BaseEntity 
{ 
    public UserGroup() 
    { 
     Users = new List<User>(); 
     UserInGroups = new List<UserInGroup>(); 
    } 

    [StringLength(255)] 
    public string Name { get; set; } 

    public string KeyName { get; set; } 

    public List<User> Users { get; set; } 

    public List<UserInGroup> UserInGroups { get; set; } 

} 

public class UserInGroup : BaseEntity 
{ 
    public UserInGroup() 
    { 
     Users = new List<User>(); 
     UserGroups = new List<UserGroup>(); 
    } 

    public int UserId { get; set; } 

    public User User { get; set; } 

    public int UserGroupId { get; set; } 

    public UserGroup UserGroup { get; set; } 

    public List<User> Users { get; set; } 

    public List<UserGroup> UserGroups { get; set; } 

} 

的DbContext:

public DbSet<GlobalSettings> GlobalSettings { get; set; } 
    public DbSet<Role> Roles { get; set; } 
    public DbSet<User> Users { get; set; } 
    public DbSet<UserGroup> UserGroups { get; set; } 
    public DbSet<UserInGroup> UsersInGroups { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<GlobalSettings>().Property(x => x.Key).HasColumnAnnotation("Index", new IndexAnnotation(new[] { new IndexAttribute("Index_VariablenName") { IsClustered = false, IsUnique = true } })); 
    } 
+0

你的情況在哪裏?和你的映射? – DevilSuichiro

+2

你不應該在UserGroup中有'List Users',你不應該在UserInGroup中擁有集合。如果我很好地展現你的模型,用戶只能通過UserInGroup加入一個組。 – tschmit007

+0

我已將DB上下文添加到頂端帖子。我創建的數據庫沒有問題,UserInGroups確實擁有用戶和組表的外鍵。只有我面臨的問題是我不知道如何獲取代碼中的數據。 @ tschmit007是用戶只能在UserInGroup中加入組 我從User和UserGroup中刪除了列表

回答

1
public abstract partial class BaseEntity 
{ 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int Id { get; set; } 
} 

public class User : BaseEntity 
{ 
    public string Username { get; set; } 
    public string Title { get; set; } 
    public FirstName { get; set; } 
    public string LasName { get; set; } 
    public Genders Gender { get; set; } 
    public string Email { get; set; } 
    public int RoleId { get; set; } 
    public string TechUser { get; set; } 
    public DateTime TechChangeDate { get; set; } 
    public int TechVersion { get; set; } 
    public bool IsDeleted { get; set; } 

    public virtual Role Role { get; set; } 
    public virtual ICollection<UserInGroup> UserInGroups { get; set; } 
} 

public class UserInGroup : BaseEntity 
{ 
    public int UserId { get; set; } 
    public int UserGroupId { get; set; } 
    public string TechUser { get; set; } 
    public DateTime TechChangeDate { get; set; } 
    public int TechVersion { get; set; } 
    public bool IsDeleted { get; set; } 

    public virtual User User { get; set; } 
    public virtual UserGroup UserGroup { get; set; } 
} 

public class UserGroup : BaseEntity 
{ 
    public string Name { get; set; } 
    public string KeyName { get; set; } 
    public string TechUser { get; set; } 
    public DateTime TechChangeDate { get; set; } 
    public int TechVersion { get; set; } 
    public bool IsDeleted { get; set; } 
} 

public class Role : BaseEntity 
{ 
    public string Name { get; set; } 
} 

public enum Genders 
{ 
    Male = 1, 
    Female = 2 
} 
+0

Ty,我現在明白了連接,非常感謝。 –

1

您可以使用兩種方法來填補導航性能。首先是延遲加載,其次是顯式指定所需的屬性。

對於延遲加載,你應該virtual申報您的導航性能:

public class User : BaseEntity 
{ 
    public int RoleId { get; set; } 

    public virtual Role Role { get; set; } 

    public virtual UserGroup UserGroup { get; set; } 

    public virtual UserInGroup UserInGroup { get; set; } 
} 

public class UserGroup : BaseEntity 
{ 
    public UserGroup() 
    { 
     Users = new HashSet<User>(); // HashSet is more effective than List 
     UserInGroups = new HashSet<UserInGroup>(); 
    } 

    [StringLength(255)] 
    public string Name { get; set; } 

    public string KeyName { get; set; } 

    public virtual ICollection<User> Users { get; set; } // ICollection is less restrective 

    public virtual ICollection<UserInGroup> UserInGroups { get; set; } 
} 

現在,您可以加載F.E.單用戶:

var justUser = dbContext.Users.Single(u => u.Id == 100); 

當你需要它的屬性,他們將通過透明地加載:

foreach (var userInGroup in user.UsersInGroups) // here is second loading 
{ 
    . . . 
} 

第二種方式是Include方法明確指定需要的屬性的召喚:

var userWithGroups = dbContext.Users 
           .Include(u => u.UserInGroups) // include single navigation property 
           .Include(ugs => ugs.Groups.Select(ug => ug.Group)) // include collection navigation property 
           .Single(u => u.Id == 100); // get the user with specified id and filled specified properties 
+0

你的文章是真正澄清的東西,TY。 –

+0

順便說一句,我怎麼能只得到未刪除的UserInGroup結果,這是不能用.Include從我看到 –

+0

@azzaidz刪除的記錄不應該出現在結果集中。如果你的意思是,如何附加額外的條件來查詢,除了'Include':'Users.Include(u => u.UserInGroups).Where(u => u.UserInGroups。包含(...))'。 –