如何正确实现其他实体对身份用户的引用? [英] How to correctly implement other entity's reference to Identity user?

查看:79
本文介绍了如何正确实现其他实体对身份用户的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Identity which has his own context.

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

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

我还有其他一些这样的实体

Also I have some other entities like this

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

我的其他上下文所使用的

which are used by my other context

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

问题.我希望我的Comment实体具有author属性,所以我将拥有类似的

Question. I want that my Comment entity had author property, so I'll have something like

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

but ApplicationUser is located in different context, though in the same DB. I bet this is impossible.

如何正确执行此操作? 我应该将DbSet从MyContext移到IdentityContext,这样我就可以自由使用这样的代码

How to correctly implement this? Should I move DbSets from MyContext to IdentityContext, so I can freely use code like this

public virtual ApplicationUser Author {get;set;}

或者我应该将其保留在不同的上下文中,但是添加类似的内容

or should I leave it be in different context, but add something like

public string AuthorId {get;set}

,并提供一些变通办法来每次需要时从不同的上下文中获取作者信息?还是其他?

and make some workarounds to get the author info from different context every time I need it? Or something else?

谢谢

修改

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

Ok, I ended up with something like this:

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填充到代码中的某个地方吗?

But how I should implement Comment's author reference? Like this? So it will not be linked via EF relationships and I'll just fill UserProfileId somewhere in code by myself?

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

这是正确的方法吗?

推荐答案

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

Ask yourself the question, what information does ApplicationUser have that you can use in your business model? And if so, is that the right location to store it? Or do you just want to link the user?

ApplicationUser位于同一数据库中,但位于不同的上下文中.

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

但是现在假设不是.假设您将来希望使用类似IdentityServer的工具.

But now suppose it is not. Suppose you want to use something like IdentityServer in the future.

我认为最好的方法是将您的业务信息与身份信息分开保存.我不想以可能被读取或更改的身份向企业公开登录信息.

I think the best approach would be to keep your business information seperated from the identity information. I wouldn't want to expose login information to the business with the posibility that it may be read or changed.

我已经看到了将ViewView中的ApplicationUser(作为业务上下文的一部分)发送给客户端的代码,包括HashPassword.确定要阻止的事情.

I've seen code where the ApplicationUser (as part of the business context) was sent in a ViewModel to the client, including HashPassword. Definately something you want to prevent.

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

What you can do is add a User table in MyContext to store the data you want to use. The ApplicationUser doesn't have any information you want in your business model.

我想您所要做的就是将信息链接到用户.而且您想从Entiy Framework的对象链接中受益.

I assume all you want is to link the information to a user. And you want to profit from Entiy Framework's object linking.

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

So create a User table and add a property to ApplicationUser to store the UserId of your User table. Or you can link the other way around: add ApplicationUserId to your User table. It is also possible to use the same Id for both: set ApplicationUser.Id yourself (doesn't have to be a guid) or use the generated guid for User.Id.

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

In case there is some additional information in the identity context that you want to use, e.g. EmailAddress, you may consider to add claims.

-更新-

这个想法是将用户表添加到您的上下文中,而不是身份上下文中.为了更清楚一点,我将称呼表Person(不是user).请注意,Person不会继承IdentyUser/ApplicationUser.

The idea is to add a user table to your context, not the identity context. To make it more clear I will call the table Person (not user). Notice that Person does not inherit 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()).

Now when I query all comments for the current user I can lookup the Person.Id (based on User.Identity.GetUserId()).

创建登录名时,请不要忘记添加一个人.

Do not forget to add a Person when you create a login.

我希望这会有所帮助.如果没有,请告诉我.

I hope this helps. If not, please let me know.

这篇关于如何正确实现其他实体对身份用户的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆