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

问题描述 投票:0回答:2

我在尝试弄清楚如何从实体框架重命名和配置自动生成的表时遇到麻烦。

这是我的代码:

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

这些实体迁移到数据库后所表达的结果如下:

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

提前致谢。干杯。

c# asp.net entity-framework asp.net-identity
2个回答
1
投票

您正在使用代码优先方法,这会跟踪与该模型对应的模型和数据库表。因此,您无法更改模型。选项是启用迁移或禁用模型检查。
检查此链接进行迁移
https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/migrations-and-deployment-with -asp-net-mvc 应用程序中的实体框架


0
投票

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

感谢您的回答。

© www.soinside.com 2019 - 2024. All rights reserved.