如何正确实现其他实体对Identity用户的引用?

我正在使用具有自己上下文的Identity。

public class ApplicationUser : IdentityUser { // some custom fields } public class IdentityContext : IdentityDbContext { //... } 

我也有一些像这样的其他实体

 public class Comment{ public int Id {get;set;} public string Message{get;set;} public DateTime Time{get;set;} } 

这是我的其他背景使用的

 public class MyContext :DbContext { public DbSet Comments { get; set; } //... other DbSets } 

题。 我希望我的评论实体有作者属性,所以我会有类似的东西

 public class Comment{ public int Id {get;set;} public string Message{get;set;} public DateTime Time{get;set;} public virtual ApplicationUser Author {get;set;} } 

但ApplicationUser位于不同的上下文中,但在同一个DB中。 我打赌这是不可能的。

如何正确实现这个? 我应该将DbSets从MyContext移动到IdentityContext,所以我可以自由地使用这样的代码

 public virtual ApplicationUser Author {get;set;} 

或者我应该把它留在不同的背景下,但添加类似的东西

 public string AuthorId {get;set} 

并做一些变通方法,以便每次需要时从不同的上下文中获取作者信息? 或者是其他东西?

谢谢


编辑

好的,我最终得到了这样的东西:

 public class ApplicationUser : IdentityUser { public virtual UserProfile UserProfile { get; set; } } public class UserProfile { [Key, ForeignKey("ApplicationUser")] public string Id { get; set; } //... custom fields public virtual ApplicationUser ApplicationUser { get; set; } } public class IdentityContext : IdentityDbContext { //... public DbSet UserProfiles { get; set; } } 

但是我应该如何实现Comment的作者参考? 像这样? 所以它不会通过EF关系链接,我只是自己填写代码中的UserProfileId?

 public class Comment{ public int Id {get;set;} public string UserProfileId{get;set;} } 

这是正确的方法吗?

问自己一个问题, ApplicationUser可以在您的业务模型中使用哪些信息? 如果是这样, 那是存放它的正确位置吗? 或者你只是想链接用户?

ApplicationUser位于不同的上下文中,但位于同一个DB中。

但现在假设它不是。 假设您希望将来使用IdentityServer之类的东西。

我认为最好的方法是将您的业务信息与身份信息分开 。 我不希望将登录信息公开给业务,并且可以读取或更改它。

我见过代码,其中ApplicationUser(作为业务上下文的一部分)在ViewModel中发送到客户端,包括HashPassword。 绝对是你想要阻止的东西。

您可以做的是在MyContext中添加一个User表来存储您要使用的数据。 ApplicationUser在您的业务模型中没有您想要的任何信息。

我假设你想要的只是将信息链接到用户。 而且您希望从Entiy Framework的对象链接中获益。

因此,创建一个User表并向ApplicationUser添加一个属性以存储User表的UserId。 或者您可以反过来链接:将ApplicationUserId添加到您的User表。 也可以对两者使用相同的Id:自己设置ApplicationUser.Id(不必是guid)或者为User.Id使用生成的guid。

如果您要使用的身份上下文中有一些其他信息,例如EmailAddress,您可以考虑添加声明。

– 更新 –

我们的想法是在您的上下文中添加用户表,而不是标识上下文。 为了使它更清楚,我将调用表Person(而不是用户)。 请注意,Person不会inheritanceIdentyUser / ApplicationUser。

 public class Person { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } //etc.. public string ApplicationUserId { get; set; } } public class MyContext :DbContext { public DbSet Comments { get; set; } public DbSet Persons { get; set; } //... other DbSets } public class Comment{ public int Id {get;set;} public string Message{get;set;} public DateTime Time{get;set;} public virtual Person Author {get;set;} } 

现在,当我查询当前用户的所有注释时,我可以查找Person.Id(基于User.Identity.GetUserId())。

创建登录时不要忘记添加Person。

我希望这有帮助。 如果没有,请告诉我。

例:

 public class ApplicationUser : IdentityUser { // some custom fields } public class Comment{ public int Id {get;set;} public string Message{get;set;} public DateTime Time{get;set;} public string AuthorId {get;set} [ForeignKey("AuthorId")] public virtual ApplicationUser Author {get;set;} } public class MyContext :IdentityDbContext { public MyContext(): base("DefaultConnection", false){ } public DbSet Comments { get; set; } //... other DbSets } 

我总是流动这种模式。 希望它对你有所帮助。