【问题标题】:Entity Framework mapping between ASP Identity user and other tables - change Id columnASP Identity 用户和其他表之间的实体框架映射 - 更改 Id 列
【发布时间】:2014-06-29 18:21:59
【问题描述】:

我的应用中有一个标准的Identity 用户...

public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{...

...以Id 作为主键。 如何将Id 更改为 UserId?
我问这个是因为我有应该与AspNetUsers 表连接的表。 我是这样做的:

public class ApplicationUser : IdentityUser<int, ApplicationUserLogin, ApplicationUserRole, ApplicationUserClaim>
{...
public virtual ICollection<JobApply> JobApplies { get; set; }

JobApply 代码是:

public class JobApply
{...
public int ApplicationUserId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }

这样的结果是这样的:

我想以某种方式使它变得更好。
我想要 UserId 而不是 Id 所以两个表中的列都是名称 UserId。
有没有办法做到这一点?

【问题讨论】:

    标签: c# asp.net entity-framework entity-framework-6 asp.net-identity


    【解决方案1】:

    您无法更改名称。 Id 定义在扩展系统的最底部IUser&lt;T&gt;,所有IdentityUser 实现最终都必须从它继承。

    如果不重写所有内容,就没有办法做到这一点。

    您可以更改存储在数据库中的字段名称,但不能更改代码中使用的属性名称。如果只想更改数据库中列的名称,则只需使用 FluentConfiguration 更改属性名称即可。

    编辑:

    在您的 ApplicationContext 中,您可以这样做:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    
        modelBuilder.Entity<ApplicationUser>()
            .Property(p => p.Id)
            .HasColumnName("UserId");
    }
    

    【讨论】:

    • 你想说生成数据库表表JobApply有列UserId之后就没有办法了,必须是ApplicationUserId?不重写所有内容就跑题了:)
    • then you need only use FluentConfiguration to change the name of the property 你能给我一个简短的例子吗?
    • @1110 - 您不是要求 JobApply 成为 UserId,而是要求 IdentityUser 为 Id 使用不同的名称。
    • 另一个问题来自这个答案stackoverflow.com/questions/24475351/…
    • 我有类似的情况,但我希望在 [Register] stackoverflow.com/questions/42663471/… 上创建用户时在 id 中输入用户 ID 值,请有人帮忙..
    猜你喜欢
    • 1970-01-01
    • 2016-02-21
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 2016-10-11
    相关资源
    最近更新 更多