使用Identity 2和Entity Framework 6 IdentityUserRoles表额外的列/场 [英] Extra column/field on IdentityUserRoles table using Identity 2 and Entity Framework 6

查看:144
本文介绍了使用Identity 2和Entity Framework 6 IdentityUserRoles表额外的列/场的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的项目中IdentityUser类的自定义实现。结果
问题是,实体框架表中增加了一个额外的列的 IdentityUserRoles ,映射子类ID作为约束条件。结果
 我知道,EF可能是混得盘算如何映射这个新阶级的关系,我只是不知道为什么,以及如何解决它混淆。我一直在寻找这个EF行为的网站,但无法找到解决的办法呢。

I'm using a custom implementation of the IdentityUser class in my project.
The problem is that Entity Framework adds an extra column in the table IdentityUserRoles, mapping the child class Id as a constraint.
I know that EF is probably getting confused on figuring how to map the relationship with this new class, I just don't know why and how to solve it. I've been searching the web about this EF behavior, but couldn't find a solution yet.

下面是一些截图汇总问题:

Here are some screenshots that summarize the problem:





推荐答案

所以,我重新检查我的数据库映射,我想我已经找到了一个解决这个问题:

So, I've rechecked my database mappings, and I think I have found a solution for this problem:

我的用户类映射已位于另一个文件(UsuarioMap),从EntityTypeConfiguration继承的,那么我会在其构造指定我的配置,酷似在后的形象。 (恕我直言,这是code组织更好)。现在我IdentityUser(Usuario)表配置它只是位于 OnModelCreating 方法中:

My user class mapping was located in another file (UsuarioMap), that inherited from EntityTypeConfiguration, and then I would specify my configurations in its constructor, exactly like the image in the post. (IMHO this is better for code organization). Now my IdentityUser (Usuario) table configuration it's simply located inside the OnModelCreating method:

        modelBuilder.Entity<Usuario>().ToTable("Usuario");
        modelBuilder.Entity<Usuario>().Property(u => u.UserName).IsRequired();
        modelBuilder.Entity<Usuario>().Property(u => u.Email).IsRequired();
        modelBuilder.Entity<Usuario>().Property(u => u.PasswordHash).IsRequired();
        modelBuilder.Entity<Usuario>().Property(u => u.Nome).IsRequired();
        modelBuilder.Entity<Usuario>().Property(u => u.DataRegistro).IsRequired();
        modelBuilder.Entity<Usuario>().Property(u => u.UltimoLogin).IsOptional();
        modelBuilder.Entity<Usuario>().HasOptional(u => u.Cliente).WithMany(c => c.Usuarios);

老实说,我不知道为什么实体框架看这两个配置路径以来的参数有所不同的 modelBuilder.Configurations.Add()并在返回的 modelBuilder.Entity() System.Data.Entity.ModelConfiguration.EntityTypeConfiguration 对象,所以逻辑上左右逢源应该工作。

I honestly don't know why Entity Framework look at this two configurations paths differently since the parameter of modelBuilder.Configurations.Add() and the return of modelBuilder.Entity() is a System.Data.Entity.ModelConfiguration.EntityTypeConfiguration object, so logically both ways should work.

这篇关于使用Identity 2和Entity Framework 6 IdentityUserRoles表额外的列/场的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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