【问题标题】:Why do I have two tables - AspNetUsers and ApplicationUser为什么我有两个表 - AspNetUsers 和 ApplicationUser
【发布时间】:2019-06-05 07:29:23
【问题描述】:

我是 ASP.NET Core MVC 和 EF 的新手。我创建了一个CatchmebgUser,它继承了IdentityUser。我在CatchmebgUser 中为照片添加了一个自定义字段并应用了迁移。

现在我有两个表:AspNetUsersCatchmebgUser

使用 User 对象时,我从 AspNetUsers 表中获取数据,直到我将此行添加到 CatchmebgContext

builder.Entity<CatchmebgUser>().ToTable("CatchmebgUser");

有没有办法摆脱AspNetUsers 表或者我应该为用户提供两个单独的表?

【问题讨论】:

  • 如果我是你,我会建议离开 AspNetUsers 表。从这里您可以获得控制器的角色和身份验证。此外,删除它很可能会导致必须删除其他 AspNet 表。

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


【解决方案1】:

如果您从IdentityUser 继承自定义User 类型,则必须改用IdentityDbContext 的派生版本,它允许您为整个身份框架中使用的用户、角色和主键指定以下具体类型:

public class MyDbContext : IdentityDbContext<CatchmebgUser, IdentityRole, string>
{
   ...
}

这样您就不必为您的类型添加builder.Entity&lt;CatchmebgUser&gt;().ToTable("CatchmebgUser") 配置,因为上下文会自动将DbSet&lt;CatchmebgUser&gt; 用于Users 属性。

【讨论】:

  • 我已经有了:public class catchmebgContext : IdentityDbContext{} 但我还有两个表
  • 然后只需删除您手动添加的DbSet 属性,没有它就可以了 - 然后它应该只生成一个AspNetUsers
  • 我的意思是删除builder.Entity&lt;CatchmebgUser&gt;().ToTable("CatchmebgUser");
  • 请在删除builder 行后添加一个新的迁移,这应该会删除该表。或者完全重置迁移(以防您仍在进行原型设计并且不会丢失数据)。
  • 如果这解决了您的问题,请考虑接受它作为答案,以便将其关闭。快乐编码:-)!
猜你喜欢
  • 2015-03-26
  • 1970-01-01
  • 2015-06-19
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 2021-11-07
  • 2015-03-29
相关资源
最近更新 更多