简体   繁体   English

如何将自定义 model 中的 map 列标识为 Z9E0DA8438E1E38A1C30F4B76CE7 中的标识 model

[英]How to map columns in Custom model to Identity model in ASP.NET Core Identity

I am implementing ASP.NET Core Identity in an existing project with an existing Database.我正在使用现有数据库的现有项目中实施 ASP.NET 核心标识。

I have specified the Entities to use for IdentityUser and IdentityRole like this:我已经指定了要用于IdentityUserIdentityRole的实体,如下所示:

public partial class MyContext : IdentityDbContext<MyUser, MyRole, int>

In Startup.cs :Startup.cs中:

services.AddIdentity<MyUser, MyRole>(options =>
        {
            options.User.RequireUniqueEmail = true;
        }).AddEntityFrameworkStores<MyContext>();

I was able to apply the migrations successfully to the database.我能够成功地将迁移应用到数据库。 But, on inspecting the AspNetRole table, I could see that EF created some extra columns.但是,在检查 AspNetRole 表时,我可以看到 EF 创建了一些额外的列。

在此处输入图像描述

How do I tell EF Core to use the existing colums (like RoleId for Id in the above image) instead of creating new ones?如何告诉 EF Core 使用现有的列(如上图中的RoleId for Id )而不是创建新的列?

Also, I noticed that EF Core changed MyRoles table name to AspNetRoles which was not the case with the MyUsers table.此外,我注意到 EF Core 将MyRoles表名更改为AspNetRoles ,而MyUsers表并非如此。 MyUsers table name remained the same even though extra columns were created as I mentioned above.即使如上所述创建了额外的列, MyUsers表名也保持不变。 Why is that?这是为什么?

PS: I am using DotNet Core 3.0 with EF Core 3.0. PS:我正在使用 DotNet Core 3.0 和 EF Core 3.0。

  1. Add [Column()] annotations to change the default column name:添加[Column()]注释以更改默认列名称:

     public class MyRole: IdentityRole<int> { [Column("RoleId")] public override int Id { get; set; } [Column("RoleName")] public override string Name { get; set; } }
  2. To reuse the old table MyRoles , custom the OnModelCreating(builder) method:要重用旧表MyRoles ,请自定义OnModelCreating(builder)方法:

     public class AppIdentityDbContext: IdentityDbContext<MyUser, MyRole, int> {... protected override void OnModelCreating(ModelBuilder builder) { base.OnModelCreating(builder); builder.Entity<MyRole>(e =>{ e.ToTable("MyRoles"); }); } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM