[英]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.