C# 将ASP.NET标识集成到现有数据库

C# 将ASP.NET标识集成到现有数据库,c#,asp.net,asp.net-mvc,entity-framework,asp.net-identity,C#,Asp.net,Asp.net Mvc,Entity Framework,Asp.net Identity,我对所有这些实体框架的东西都是新手,所以请原谅我 我正在尝试将ASP.NET标识集成到我的项目中。我已经有了一个已映射并准备就绪的现有数据库,我将首先使用代码进行自动迁移。我要做的是让Identity只存储用户名、密码和一些其他字段,其余信息需要存储在另一个表中,称为Contact,因此我需要从我的Identity到我的表的1:1连接 到目前为止,我尝试将所有内容合并到一个上下文中,因此我得到的是: public partial class pmdb02Context : IdentityDbC

我对所有这些实体框架的东西都是新手,所以请原谅我

我正在尝试将ASP.NET标识集成到我的项目中。我已经有了一个已映射并准备就绪的现有数据库,我将首先使用代码进行自动迁移。我要做的是让Identity只存储用户名、密码和一些其他字段,其余信息需要存储在另一个表中,称为Contact,因此我需要从我的Identity到我的表的1:1连接

到目前为止,我尝试将所有内容合并到一个上下文中,因此我得到的是:

public partial class pmdb02Context : IdentityDbContext
{
    static pmdb02Context()
    {
        Database.SetInitializer<pmdb02Context>(null);
    }

    public pmdb02Context()
        : base("pmdb02Context")
    {
    }

    --- Lots of DbSets ---

    public DbSet<ApplicationUser> ApplicationUsers { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        --- Lots of Mappings ---

        modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
        modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
        modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });
        modelBuilder.Entity<ApplicationUser>().ToTable("IdentityUsers", "dbo");    
    }
}
我收到这个:

IdentityUser_Logins_Target: : Multiplicity is not valid in Role 'IdentityUser_Logins_Target' in relationship 'IdentityUser_Logins'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:
我的ApplicationUser类如下所示:

public class ApplicationUser : IdentityUser
{
    public int ContactId { get; set; }
    public bool? IsCurrent { get; set; }

    public virtual Contact Contact { get; set; } 
}
所以,我需要一个联系的关系。 我想,我的问题是,我一直在努力让这些东西一起工作,昨天和今天我都在努力这样做

我真的很感激任何帮助。
谢谢

创建用户时,还必须创建其联系人。所以如果以后会创建联系人 将用户和联系人之间的关系更改为1到(1..0)


昨天终于解决了我的问题。 结果表明,我的问题的主要来源是:

Database.SetInitializer<pmdb02Context>(null);
Database.SetInitializer(null);
当我把它改成:

Database.SetInitializer<pmdb02Context>(new MigrateDatabaseToLatestVersion<pmdb02Context, Configuration>());
Database.SetInitializer(新的MigrateDatabaseToLatestVersion());

身份最终按预期工作,我只是像往常一样将关系添加到联系人中。

您好,谢谢,我在联系人中创建了一个用户,忘了提及这一点。我将关系更改为您所说的关系,但没有更改,我仍然得到相同的异常。可以告诉您从何处获得
配置
?在Package Manager控制台中启用写入迁移,配置类将自动生成。
var user db.users.Single(u => u.Id == id);
contact.User = user;
Database.SetInitializer<pmdb02Context>(null);
Database.SetInitializer<pmdb02Context>(new MigrateDatabaseToLatestVersion<pmdb02Context, Configuration>());