如何使用数据库第一个实体框架更改表名

时间:2022-01-31 09:51:28

This is similar to this question but with using database first entity framework instead of code first: How can I change the table names when using Visual Studio 2013 ASP.NET Identity?

这与此问题类似,但使用数据库第一个实体框架而不是代码优先:如何在使用Visual Studio 2013 ASP.NET身份时更改表名?

In the code first approach you can override OnModelCreating and do something like this so that the user info is saved in MyUsers table instead of the default AspNetUsers:

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

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

In the database first approach I have manually created the MyUsers table but because OnModelCreating does not get called I don't know how to configure the data to be saved into my new table?

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

1 个解决方案

#1


0  

You can follow the Code First approach an when overriding the OnModelCreating add the line:

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

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

The line above will link the AspNetIdentity entities to your tables without re-creating the tables.

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

Code Example:

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

#1


0  

You can follow the Code First approach an when overriding the OnModelCreating add the line:

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

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

The line above will link the AspNetIdentity entities to your tables without re-creating the tables.

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

Code Example:

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