Code First Entity Framework中的Seed方法不填充多对多关系中的链接表

时间:2016-06-20 03:25:48

标签: c# entity-framework

我是实体框架代码的新手。我试图在用户和角色之间创建多对多关系。链接表将是UserRole。以下是我的代码:

public class User
    {
        public int UserId { get; set; }

    [Required]
        [MaxLength(100)]

    public string Username { get; set; }

    [Required]
        [MaxLength(100)]

    public string Email { get; set; }

    public string HashedPassword { get; set; }

    public string Salt { get; set; }

    public bool IsLocked { get; set; }

    public DateTime DateCreated { get; set; }

        public virtual ICollection<Role> Roles { get; set; }

    }

public class Role
    {
        public int RoleId { get; set; }
        public string RoleName { get; set; }

        public virtual ICollection<User> Users { get; set; }
    }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
{
    modelBuilder.Entity<User>()
            .HasMany(r => r.Roles)
            .WithMany(u => u.Users)
            .Map(ur =>
            {
                ur.MapLeftKey("UserId");
                ur.MapRightKey("RoleId");
                ur.ToTable("UserRole");
            }
        );
}

protected override void Seed(LabRating.Data.LabRatingContext context)
    {
    context.UserSet.AddOrUpdate(u => u.Email, new User[]{

    new User()
            {
                Email = "abc.blog@gmail.com",

        Username = "abc123",

        HashedPassword = "XwAQoiq84p1RUzhAyPfaMDKVgSwnn80NCtsE8dNv3XI=",

        Salt = "mNKLRbEFCH8y1xIyTXP4qA==",

                IsLocked = false,

                DateCreated = DateTime.Now

    }
        });

    context.RoleSet.AddOrUpdate(new Role[] {
            new Role()
            {
                RoleName= "Admin"
             }
            });
}

正如您所看到的,我首先创建了两个表-User和Role - 然后我在OnModelCreating方法中使用Fluent API建立多对多关系。当我检查数据库时,我看到正确创建了所有三个表 - User,Role和UserRole。运行Seed方法时,仅使用上面显示的数据填充User和Role表,而不填充UserRole表。请告诉我如何解决此问题。

1 个答案:

答案 0 :(得分:0)

protected override void Seed(DatabaseContext context)
{
    Role role = new Role
    {
        RoleName = "Admin"
    };
    Role role2 = new Role
    {
        RoleName = "HR"
    };
    Role role3 = new Role
    {
        RoleName = "Marketing"
    };

    var roles= new List<Role> { role, role2,role3 };
    roles.ForEach(i => context.Roles.AddOrUpdate(i));
    context.SaveChanges();

    User user= new User
    { 
        Email = "abc.blog@gmail.com",
        Username = "abc123",
        HashedPassword = "XwAQoiq84p1RUzhAyPfaMDKVgSwnn80NCtsE8dNv3XI=",
        Salt = "mNKLRbEFCH8y1xIyTXP4qA==",
        IsLocked = false,
        DateCreated = DateTime.Now,
        Roles= new List<Role> { role,role2 }
    };

    User user2 = new User
    { 
        Email = "def.blog@gmail.com",
        Username = "def123",
        HashedPassword = "XwAQoiq84p1RUzhAyPfaMDKVgSwnn80NCtsE8dNv3XI=",
        Salt = "mNKLRbEFCH8y1xIyTXP4qA==",
        IsLocked = false,
        DateCreated = DateTime.Now,
        Roles= new List<Role> { role3 }
    };

    var users= new List<User> { user, user2};
    users.ForEach(a => context.Users.AddOrUpdate(a));
    context.SaveChanges();
}