【发布时间】:2017-04-03 07:08:08
【问题描述】:
我目前正在重构为用户访问同一数据库的多个应用程序。每个人都向用户添加了自己的特殊属性,这会导致很多空值……我不喜欢这样。我的想法是为每个应用程序使用自己的 IdentityUser、dbContext 和 UserManager。 有一个基础 IdentityUser ,其他的从它派生。它看起来像这样:
基础用户:
public class User : IdentityUser
{
public string Username {get;set;}
public string Email {get;set;}
...
}
该用户的 dbContext 和 userManager:
public class UserAuthenticationDbContext : IdentityDbContext<User>
{
//code here
}
public class UserManager : UserManager<User>
{
//code here
}
对于第二个用户,我做了以下操作:
[Table("User2")]
public class User2 : User
{
public string SpecialProperty1 {get;set;}
public string SpecialProperty2 {get;set;}
}
public class User2Manager : UserManager<User2>
{
// code here
}
public class User2AuthenticationDbContext : IdentityDbContext<User2>
{
//code here
}
例如,User2 表只有两个属性。 User2 中的主键与用户表中的主键相同,也是它的 FK。
在 asp.net mvc 项目中,我仅使用与 User2 相关的类。如果我现在启动此应用程序,我会立即收到错误消息“无效的列名 SpecialProperty1 和 SpecialProperty2”。 实体框架似乎可以访问没有此附加字段的用户表。它不会将其与 User2 表“合并”并返回 User2 实体。 我能做些什么来实现这一点,或者有没有不同/更好的方法来做到这一点?
我的主要目标是让这多个应用程序更加干净和独立,同时所有应用程序都访问同一个数据库。
【问题讨论】:
标签: c# asp.net entity-framework asp.net-identity