Skip to main content
 首页 » 编程设计

c#之在 EF 中集成和扩展 .NET 标识类(代码优先)

2024年02月05日7mengfanrong

我正在开始一个全新的 .NET MVC 项目,针对 EF 6 和 .NET 4.5 使用代码优先。我也想使用身份框架,并在我的项目的上下文配置中扩展身份基类。 (不确定我说的是否正确...我是一名经验丰富的 C#/.NET 开发人员,对 MVC、EF 和 Identity 还有些陌生。)

我的问题:出了点问题。在尝试生成新迁移时,出现错误:

POST.Data.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. 
POST.Data.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. 
IdentityUserLogins: EntityType: EntitySet 'IdentityUserLogins' is based on type 'IdentityUserLogin' that has no keys defined. 
IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined. 

好的,那么,我的项目中有两个上下文,一个用于标识类,另一个用于其他所有内容。相同的数据库,并且任何一个上下文中的任何内容都不会引用另一个中的任何内容。我这样定义实体:

public class ApplicationUser : IdentityUser { } 
public class ApplicationRole : IdentityRole { } 
public class ApplicationUserRole : IdentityUserRole { } 
public class ApplicationUserClaim : IdentityUserClaim { } 
public class ApplicationUserLogin : IdentityUserLogin { } 

然后,我有一个这样的上下文类:

public partial class IdentityContext : IdentityDbContext 
{ 
    public IdentityContext() : base() { } 
 
    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
        base.OnModelCreating(modelBuilder); 
        modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(ul => ul.UserId); 
        modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id); 
        modelBuilder.Entity<IdentityUserRole>().HasKey(ur => new { ur.RoleId, ur.UserId }); 
    } 
 
    public DbSet<ApplicationUser> ApplicationUsers { get; set; } 
    public DbSet<ApplicationUserClaim> ApplicationUserClaims { get; set; } 
    public DbSet<ApplicationUserLogin> ApplicationUserLogins { get; set; } 
    public DbSet<ApplicationRole> ApplicationRoles { get; set; } 
    public DbSet<ApplicationUserRole> ApplicationUserRoles { get; set; } 
} 

不确定它是否重要,但我的配置看起来像这样:

internal sealed class Configuration : DbMigrationsConfiguration<POST.Data.Models.Context> 
{ 
    public Configuration() 
    { 
        AutomaticMigrationsEnabled = false; 
        MigrationsDirectory = @"ContextMigrations"; 
    } 
 
    protected override void Seed(POST.Data.Models.Context context) {} 
} 

那会构建,但我无法在没有这些错误的情况下生成迁移,并且由于我对所有细节的知识和理解有限,我感到困惑。

请您参考如下方法:

因为您没有为用户、角色等扩展任何身份实体,所以我不会费心创建您自己的身份子类(ApplicationUser、ApplicationRole 等)。您也不需要在上下文中定义任何与身份相关的 DbSet,因为它们会自动从 IdentityDbContext 继承。但是,如果您想保留您的身份子类,您的 DbContext 应该从通用 IdentityDbContext 继承,因为这允许您定义您的自定义身份实体:

public class IdentityContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim> 
{ 
   // Define other non-identity related dbsets 
} 

这将确保您的 DbContext 自动拥有所有相关的 DbSet 属性,而无需显式定义它们,因为它们是从通用 IdentityDbContext<> 类继承的。

您也不需要在 OnModelCreating 中为任何身份内容定义任何迁移或流畅的 api 配置,因为您没有向身份实体添加任何内容(例如,额外的 ApplicationUser 属性)。初始化 DbContext/EF 时将自动创建默认标识表。