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

查看:379
本文介绍了使用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:

我的用户类映射位于继承自EntityTypeConfiguration的另一个文件(UsuarioMap)中,然后在其中指定我的配置构造函数,完全像帖子中的图像。 (IMHO这更适合代码组织)。现在我的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);

我真的不知道为什么Entity Framework看起来这两个配置路径不同,因为 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天全站免登陆