EntityFramework代码第一个键

时间:2015-09-21 13:48:50

标签: c# entity-framework

我有以下表:User,UserGroups和UserInGroups。您可以在下面的图片中看到它们。当我呼叫用户时,我希望能够获得用户所在的组(UserInGroups)。我正在阅读有关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 } }));
    }

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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