在实体框架中配置自动生成表

迈赫迪·布阿马马

我在尝试从实体框架中找出如何重命名和配置自动生成的表时遇到了麻烦。这是我的代码:

 public class ApplicationUser : IdentityUser
 {
     public virtual List<ApplicationUser> AddingFriends { get; set; }
     public virtual List<ApplicationUser> AddedFriends { get; set; }
 }

这些实体一旦迁移到数据库中所表达的结果如下: 生成表

所以我基本上只想重命名这个表和它的列名。除此之外,我还想为具有相同实体列表的臃肿人员创建第二个表。所以基本上当我添加另外两个应用程序用户列表时,它会将这些属性绑定到如下所示的实际表。有没有办法控制这些表的生成并正确配置它们?

提前致谢。干杯。

迈赫迪·布阿马马

在查看 Fluent API 文档后,我发现我可以通过将这些代码行添加到 OnModelCreating 方法来配置它:

modelBuilder.Entity<ApplicationUser>()
            .HasMany(c => c.AddedFriends)
            .WithMany(c => c.AddingFriends)
            .Map(m =>
            {
                m.ToTable("Friends");
                m.MapLeftKey("AddedUser");
                m.MapRightKey("AddingUser");
            });
modelBuilder.Entity<ApplicationUser>()
            .HasMany(c => c.BloquedUsers)
            .WithMany(c => c.BloquingUsers)
            .Map(m =>
            {
                m.ToTable("Bloqueds");
                m.MapLeftKey("BloquingUser");
                m.MapRightKey("BloquedUser");
            });

感谢您的回答。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章