c# - 如何使用数据库优先 Entity Framework 更改表名

标签 c# asp.net .net entity-framework asp.net-identity

这类似于这个问题,但使用数据库优先 Entity Framework 而不是代码优先: How can I change the table names when using Visual Studio 2013 ASP.NET Identity?

在代码优先方法中,您可以覆盖 OnModelCreating 并执行类似这样的操作,以便将用户信息保存在 MyUsers 表中而不是默认的 AspNetUsers 中:

modelBuilder.Entity<IdentityUser>().ToTable("MyUsers")

在数据库优先方法中,我手动创建了 MyUsers 表,但由于未调用 OnModelCreating,我不知道如何配置要保存到新表中的数据?

最佳答案

您可以遵循 Code First 方法,在覆盖 OnModelCreating 时添加以下行:

System.Data.Entity.Database.SetInitializer<ApplicationDbContext>(null);

上面的行会将 AspNetIdentity 实体链接到您的表,而无需重新创建表。

代码示例:

protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<ApplicationUserRole>().ToTable("UserRoles");
    modelBuilder.Entity<ApplicationUserLogin>().ToTable("UserLogins");
    modelBuilder.Entity<ApplicationUserClaim>().ToTable("UserClaims");
    modelBuilder.Entity<ApplicationRole>().ToTable("Roles");

    System.Data.Entity.Database.SetInitializer<ApplicationDbContext>(null);
}

关于c# - 如何使用数据库优先 Entity Framework 更改表名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26170868/

相关文章:

c# - 如何: Get text index from label by mouse position? C#

c# - 从 .NET C# 运行 Gulp 任务?

ASP.NET MenuItem 单独样式

asp.net - DropDownList对于未选择值的集合

c# - 在asp.net中将图片url转换为图片bye

.net - Xamarin 身份验证 - 用户已弃用,迁移到 IAccount 时出现问题

c# - 如何模拟由给定信号驱动的谐波振荡器(不是由正弦波驱动)

c# - C# 中的接口(interface)

.net - 低速网络,断线风险高

c# - Unity - 通过 XML 的工厂