在EF中集成和扩展.NET Identity Class(代码优先)



我正在启动一个全新的.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.

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

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 等)。您也不需要在上下文中定义任何与身份相关的 DbSets,因为它们会自动继承自 IdentityDbContext。但是,如果要保留标识子类,则 DbContext 应从通用标识数据库上下文继承,因为这允许您定义自定义标识实体:

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

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

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

您可以尝试使用属性Key设置实体键。像这样:

using System.ComponentModel.DataAnnotations;
namespace YourCompany.YourProject.Models
{
    public class ApplicationUser : IdentityUser
    {
        [Key]
        public string UserId { get; set; }
    }
    public class ApplicationRole : IdentityRole
    {
        [Key]
        public string Id { get; set; }
    }
    public class ApplicationUserRole : IdentityUserRole
    {
        [Key]
        public string UserId { get; set; }
        [Key]
        public string RoleId { get; set; }
    }
    public class ApplicationUserClaim : IdentityUserClaim { }
    public class ApplicationUserLogin : IdentityUserLogin { }
}

我希望它对您有所帮助,如果确实如此,请将其标记为答案;)

相关内容

最新更新