1

我开始一个全新的.NET MVC项目,首先针对EF 6和.NET 4.5使用代码。我也想使用Identity框架,并在我的项目的上下文配置中扩展Identity基类。 (不知道我说的都是正确的......我是一位经验丰富的C#/ .NET开发人员,对于MVC,EF和身份来说有点新鲜。)在EF中集成和扩展.NET标识类(代码优先)

我的问题:有些事情是错误的。在试图产生一个新的迁移,我得到错误:

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

这将会建立起来,但是如果没有这些错误,我不能产生迁移,并且我对有限的知识和对所有细节的理解都很困惑。

回答

2

既然你不延长任何身份实体用户,角色等我不会麻烦创建自己的身份子类(ApplicationUser,ApplicationRole等)。您也不需要在上下文中定义任何与身份相关的DbSets,因为它们是从IdentityDbContext自动继承的。但是,如果你想保持你的身份子类你的DbContext应该从通用IdentityDbContext继承,因为这允许您定义自定义标识实体:

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

这将确保你的DbContext自动获得所有相关DbSet性能,而无需明确定义它们,因为它们是从通用IdentityDbContext类继承而来的。

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

+0

好的,所以这是我正在采取的原始方向,但创建了单独的子类,因为我以为我想在将来扩展它们,我现在也可以让我的生活变得轻松。但是我坦白地说,仅仅让它独自存在就是一种逻辑。 –

+0

但我有这个问题,我希望我正确地说出这个问题:如果我没有一个从IdentityDbContext继承的单独的上下文,而是从DbContext继承,那么如何创建基类的Identity类所有这些具体实体将会在我的数据库中生成?我的意思是,在某些时候,我需要说我希望创建AspNet Identity表,对吗?又怎样?我有我的其他背景像MainContext:DbContext;应该是MainContext:IdentityDbContext? –

+0

好的,没关系。我相信我有这个想法...... :-P你只需创建单独的上下文并将其保留为空。 –

2

您可以尝试使用属性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 { } 
} 

我希望它能帮助你,如果是的话,普莱舍将其标记为答案;)

+0

谢谢。我曾想过这个,但没有尝试,因为我认为那些已经在基础课程中被标记为键......这使我更接近,因为我收到的最初错误已经消失,是的。但是,我现在收到关于隐藏的构建警告(应该使用覆盖?),并且我的迁移仍然不会创建帐户:“元数据集合中已存在具有标识'Id'的项目 参数名称:项目“ –