c# - 如何正确实现其他实体对身份用户的引用?

标签 c# asp.net-mvc entity-framework asp.net-identity dbcontext

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

public class ApplicationUser : IdentityUser {
    // some custom fields
}

public class IdentityContext : IdentityDbContext<ApplicationUser> {
   //...
}

我还有一些这样的实体

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

被我的其他上下文使用

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

问题。我希望我的 Comment 实体具有 author 属性,所以我会有类似

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 位于不同的上下文中,尽管在同一个数据库中。我敢打赌这是不可能的。

如何正确实现? 我是否应该将 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<ApplicationUser> {
        //...
        public DbSet<UserProfile> UserProfiles { get; set; }
 }

但是我应该如何实现 Comment 的作者引用?像这样?所以它不会通过 EF 关系链接,我将自己在代码中的某个地方填充 UserProfileId?

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

这是正确的做法吗?

最佳答案

问问自己这个问题,ApplicationUser 有哪些信息可以用于您的业务模型?如果是这样,存储它的位置是否正确?或者您只想链接用户吗?

ApplicationUser is located in different context, though in the same DB.

但现在假设不是。假设您将来想使用类似 IdentityServer 的东西。

我认为最好的方法是将您的业务信息与身份信息分开。我不想将登录信息暴露给企业,因为它可能被读取或更改。

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

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

我假设您想要的只是将信息链接到用户。并且您想从 Entiy Framework 的对象链接中获利。

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

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

-- 更新--

想法是将用户表添加到您的上下文,而不是身份上下文。为了更清楚,我将调用表 Person(而不是用户)。请注意,Person 不会继承 IdentyUser/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<Comment> Comments { get; set; }
   public DbSet<Person> 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())。

创建登录时不要忘记添加一个人。

希望对您有所帮助。如果没有,请告诉我。

关于c# - 如何正确实现其他实体对身份用户的引用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43825825/

相关文章:

c# - LINQ 到实体 : All method not yielding the expected result

c# - 找不到名为 webdriver.json 的文件或 ID 为“WebDriver.FirefoxPreferences”的嵌入式资源

c# - 为什么 COM 事件处理程序总是空的?

c# - 如何在 Asp.net MVC 中使用 C# 访问 HTML 元素?

c# - 全局禁用 Entity Framework 中的动态代理

nhibernate - 持久性无知可以扩展吗?

c# - 为什么我的try catch在asp.net C#中不起作用

c# - 将 json 数组反序列化为 C# 列表时遇到问题

javascript - 如何访问 session 变量并在 javascript-asp.net mvc 中设置它们?

entity-framework - 使用数据库优先模型 (EF 5) 向模型添加验证