用户身份作为不同上下文中的外键

see*_*uit 5 .net c# entity-framework ef-code-first asp.net-identity

我刚刚开始了一个新的 EF 6.1 Code-First 项目。

我正在使用ApplicationDbContext继承自的Identity 2.0IdentityDbContext

public class ApplicationDbContext
    : IdentityDbContext<ApplicationUser, ApplicationRole,
    string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{
    public ApplicationDbContext()
        : base("DefaultConnection")
    {
    }

    static ApplicationDbContext()
    {
        Database.SetInitializer<ApplicationDbContext>(new ApplicationDbInitializer());
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
    // Add additional items here as needed
}
Run Code Online (Sandbox Code Playgroud)

ApplicationUser模型的上下文。

我有另一个背景, ProfileContext

public class ProfileContext : DbContext
{
    public ProfileContext() : base()
    {
        Database.SetInitializer<ProfileContext>(new DropCreateDatabaseAlways<ProfileContext>());
    }

    public DbSet<Lifestyle> Lifestyles { get; set; }
    public DbSet<CountryName> CountryNames { get; set; }
    public DbSet<Language> Languages { get; set; }
    ...
}
Run Code Online (Sandbox Code Playgroud)

其中有几个表需要与ApplicationUser. 一个很好的例子是Lifestyle

    public class Lifestyle
{
    public int Id { get; set; }
    public string Profession { get; set; }
    public string JobDescription { get; set; }
    public string TV { get; set; }
    public string HomeMovies { get; set; }
    public string Movies { get; set; }
    public string EducationLevel { get; set; }
    public string ElementarySchool { get; set; }
    public string HighSchool { get; set; }
    public string College { get; set; }
    public string Degree { get; set; }
    public string JewishEducation { get; set; }
    public string IsraelStudy { get; set; }
    public string IsraelSchool { get; set; }
    public string Yeshiva { get; set; }

    public int NativeLanguage { get; set; }
    public int SpokenLanguage { get; set; }

    [ForeignKey("Id")]
    public ApplicationUser ApplicationUser { get; set; }
    [ForeignKey("NativeLanguage")]
    public Language Language { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

但可以预见的是,我得到了错误:

在模型生成期间检测到一个或多个验证错误:Models.Profile.ApplicationUserLogin:: EntityType 'ApplicationUserLogin' 未定义键。为此 EntityType.Models.Profile.ApplicationUserRole: 定义键:EntityType 'ApplicationUserRole' 没有定义键。定义此 EntityType 的键。Lifestyle_ApplicationUser_Target_Lifestyle_ApplicationUser_Source: :引用约束的从属角色中所有属性的类型必须与主体角色中对应的属性类型相同。实体“Lifestyle”上的属性“Id”的类型与引用约束“Lifestyle_ApplicationUser”中实体“ApplicationUser”上的属性“Id”的类型不匹配。ApplicationUserLogins: EntityType: EntitySet 'ApplicationUserLogins' 基于未定义键的“ApplicationUserLogin”类型。ApplicationUserRoles: EntityType: EntitySet 'ApplicationUserRoles' 基于未定义键的类型 'ApplicationUserRole'。

因为ApplicationUser是来自ApplicationDbContextwhile的表Lifestylein ProfileContext

由于我正在使用两个上下文,并且上下文不会在 EF 中混合,那么将外键从任何表定义回ApplicationUserin 的正确方法是IdendtityDbContext什么?我认为这经常出现,我正在寻找最佳实践。