将ASP.NET Identity集成到现有数据库 [英] Integrating ASP.NET Identity to existing database

查看:177
本文介绍了将ASP.NET Identity集成到现有数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Entity Framework的所有知识都是陌生的,所以请提前打扰我.

I'm new to all this Entity Framework stuff, so excuse me in advance.

我正在尝试将ASP.NET Identity集成到我的项目中.我已经有一个现有的数据库,已映射并可以使用,并且正在使用Code First进行自动迁移.我想做的是仅拥有身份来存储用户名,密码和其他字段,其余信息需要存储在另一个表中,该表称为Contact,所以我需要从Identity到我的1:1连接桌子.

I'm trying to integrate ASP.NET Identity to my project. I've got an existing database, mapped and ready, and I'm using Code First with automatic migrations. What I'm trying to do is have Identity only to store username, password and some other fields and the rest of the information needs to be stored in another table, called Contact, so I need 1:1 connection from my Identity to my that table.

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

What I've tried so far is merging all in one context, so what I've got is:

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");    
    }
}

当我到达时:

var result = await UserManager.CreateAsync(user, model.ContactData.Password);

我收到这个:

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类如下:

My ApplicationUser class looks like this:

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

    public virtual Contact Contact { get; set; } 
}

因此,我需要与Contact建立联系. 我想,我的问题在于,我试图将这些问题变成可以相互协作的方式,我一直在努力做到这一点,昨天和今天也是如此.

So, I need a relation to Contact. My problem, I guess, is in the way I'm trying to get these to things to work together at all, I've been trying to do so all day yesterday and today as well.

非常感谢您的帮助. 谢谢!

I'd really appreciate any help. Thanks!

推荐答案

昨天终于解决了我的问题. 原来,我的问题的主要根源在以下行中:

Finally fixed my issue yesterday. Turns out the main source of my problem was in this line:

Database.SetInitializer<pmdb02Context>(null);

当我将其更改为:

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

身份终于按照预期的方式工作了,我只是像往常一样将关系添加到联系人"中.

Identity finally worked as supposed to, I only added the relationship to Contact as usual.

这篇关于将ASP.NET Identity集成到现有数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆