简体   繁体   English

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

[英]The link table in Many-to-many relationship is not populated by the Seed method in Code First Entity Framework

I am new to entity framework code first. 我是刚接触实体框架代码的新手。 I am trying to create a many-to-many relationship between User and Role. 我正在尝试在用户和角色之间建立多对多关系。 The link table will be UserRole. 链接表将为UserRole。 The following is my code: 以下是我的代码:

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"
             }
            });
}

As you can see first I am creating the two tables -User and Role - and then I am establishing the many-to-many relationship using Fluent API in the OnModelCreating method. 如您所见,我先创建两个表-User和Role-,然后在OnModelCreating方法中使用Fluent API建立多对多关系。 When I checked the database, I see all three tables - User, Role and UserRole - being created correctly. 当我检查数据库时,我看到所有三个表-User,Role和UserRole-均已正确创建。 When the Seed method is run, only the User and Role tables are populated with the data shown above but not the UserRole table. 运行Seed方法时,只会使用上面显示的数据填充User和Role表,而不会填充UserRole表。 Please let me know how to resolve this issue. 请让我知道如何解决此问题。

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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在实体代码优先中以多对多关系为数据库播种 - How to seed the database with a many-to-many relationship in Entity Code First Entity Framework 4.1 - Code First:多对多关系 - Entity Framework 4.1 - Code First: many-to-many relationship 实体框架,代码优先,如何从多对多关系映射中向表添加主键? - Entity Framework, code-first, how to add primary key to table out of many-to-many relationship mapping? 实体框架代码在单个表中的第一个多对多关系 - Entity Framework Code First Many to Many relationship(s) in single table 实体框架代码优先在同一个表上的多对多关系 - Entity Framework Code-first Many to many relationship on the same table Entity Framework Core 多对多中的种子 - Seed in Entity Framework Core Many-to-Many 种子代码首先从多对多中进行多对多 - Seed Code First Many-to-Many off of a Many-to-Many 如何首先使用Entity Framework代码创建多对多关系? - How do I create a many-to-many relationship with Entity Framework code first? 实体框架核心代码优先:级联删除多对多关系 - Entity Framework Core Code-First: Cascade delete on a many-to-many relationship 自引用多对多递归关系代码优先实体框架 - Self-referencing many-to-many recursive relationship code first Entity Framework
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM