C# 不同上下文中作为外键的用户标识

C# 不同上下文中作为外键的用户标识,c#,.net,entity-framework,ef-code-first,asp.net-identity,C#,.net,Entity Framework,Ef Code First,Asp.net Identity,我刚刚开始了一个新的EF6.1代码优先项目 我正在使用Identity 2.0,其中ApplicationDbContext继承自IdentityDbContext public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, string, ApplicationUserLogin, ApplicationUserRole, ApplicationUserCl

我刚刚开始了一个新的EF6.1代码优先项目

我正在使用Identity 2.0,其中
ApplicationDbContext
继承自
IdentityDbContext

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
}
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; }
    ...
}
其中有多个表需要与
ApplicationUser
建立一对一的关系。生活方式就是一个很好的例子:

    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; }
}
但可以预见的是,我得到了一个错误:

在模型生成过程中检测到一个或多个验证错误:Models.Profile.ApplicationUserLogin::EntityType“ApplicationUserLogin”未定义密钥。定义此EntityType.Models.Profile.ApplicationUserRole的键::EntityType“ApplicationUserRole”未定义键。定义此EntityType的键。生活方式\应用程序用户\目标\生活方式\应用程序用户\源::引用约束的从属角色中所有属性的类型必须与主体角色中相应的属性类型相同。实体“Lifestyle”上属性“Id”的类型与引用约束“Lifestyle\u ApplicationUser”中实体“ApplicationUser”上属性“Id”的类型不匹配。ApplicationUserLogins:EntityType:EntitySet“ApplicationUserLogins”基于未定义键的类型“ApplicationUserLogin”。ApplicationUserRoles:EntityType:EntitySet“ApplicationUserRoles”基于未定义键的类型“ApplicationUserRole”

因为
ApplicationUser
ApplicationDbContext
中的一个表,而
lifiety
位于
ProfileContext


由于我使用的是两种上下文,并且上下文不会在EF中混合,那么在
IdendtityDbContext
中定义从任何表返回到
ApplicationUser
的外键的正确方法是什么?我想这经常出现,我正在寻找最佳做法。

是的,这是一个常见的做法。简单的解决方案是使用单个上下文。看看克里斯的解释:哇。谢谢你找到这个问题:)我本来打算选择克里斯的第二个选择,直到我看到他的第一个。