在使用ASP .NET身份时,您是否应该在业务数据库中存储其他用户信息?
因此 - 我刚刚开始使用Newbie项目中的ASP .NET身份框架,但我偶然发现了关于用户的逻辑问题,我不知道如何解决,所以让我告诉我这是什么意思。
(它们都使用不同的DB上下文):
- 在我的Web项目中,我使用两个数据库
- 在
向项目添加身份之前,我将所有用户的信息存储在我的业务数据库中(使用EntityFrameWork core for Migripations for Migripation等),他们的模型就是这样:
public abstract class User
{
public int Id { get; set; }
[Required(ErrorMessage = "Input login")]
public string Login { get; set; }
[Required(ErrorMessage = "Input password")]
[Display(Name = "Password")]
public string Pswrd { get; set; }
[Required(ErrorMessage = "Input mail")]
[Display(Name = "E-mail")]
public string Email { get; set; }
[Display(Name = "Phone number (optional)")]
[Range(111111111, 999999999)]
public int Phone_nr { get; set; }
}
我在注册时选择了两个角色:
public class Poster : User
{
[Display(Name = "Questions asked (total)")]
public int Total_posted { get; set; }
//1 to N
public List<Question> Questions { get; set; }
}
public class Answerer : User
{
[Display(Name = "Answers posted (total)")]
public int Total_answered { get; set; }
//1 to N
public List<Question> Questions { get; set; }
}
然后 我认为我不需要在业务数据库中存储ID,密码,电子邮件之类的信息,而是在身份数据库中。
然后是我的问题和我的问题 - 是否应该通过自定义IdentityUser类来存储所有用户信息,以便将其包含一些其他属性,然后将用户表放在业务DB中?因为如果是的,那么我将如何包括外国钥匙(或实际上 - 什么是它们的替代方案),因为我不知道在两个不同的数据库之间是否有可能)?因为可以看出 - 我的用户类必须包括对其他类的参考(在我的情况下为“问题”类)。
so - I've just started working with ASP .Net Identity framework in my newbie project but I've stumbled upon a logic problem regarding users that I don't know how to resolve, so let me tell what I mean by that.
In my web project I use two databases (both of them uses different db context):
- Business database
- ASP .Net Identity database
Before adding Identity to my project, I've stored all of users informations in my business database (using EntityFramework Core for migrations etc.), their model is like this:
public abstract class User
{
public int Id { get; set; }
[Required(ErrorMessage = "Input login")]
public string Login { get; set; }
[Required(ErrorMessage = "Input password")]
[Display(Name = "Password")]
public string Pswrd { get; set; }
[Required(ErrorMessage = "Input mail")]
[Display(Name = "E-mail")]
public string Email { get; set; }
[Display(Name = "Phone number (optional)")]
[Range(111111111, 999999999)]
public int Phone_nr { get; set; }
}
And then I have two roles for my users which were selected while registering:
public class Poster : User
{
[Display(Name = "Questions asked (total)")]
public int Total_posted { get; set; }
//1 to N
public List<Question> Questions { get; set; }
}
public class Answerer : User
{
[Display(Name = "Answers posted (total)")]
public int Total_answered { get; set; }
//1 to N
public List<Question> Questions { get; set; }
}
Now - when I've added Identity to my project (along with database containing things like users, roles etc.), I figured that I won't need to store informations like id's, passwords, e-mails in my business database but in identity one.
And then comes my problem and my question - should all of user informations be stored in Identity database by customizing IdentityUser class so it will contain some additional properties and just ditch the users table in business db? Because if yes, then how would I include things like foreign keys (or actually - what would be an alternative to them, because I don't know if it will even be possible between two different databases)? Because as it can be seen - my user classes must include references to other class ("Question" class in my case).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没关系,我在这个答案中发现了需要信息(即使我之前搜索,我现在刚刚找到了整个帖子和答案) -
for-n #48624628>使用EF Core core core sidentityContext和dbContext以及dbContext都进行订单管理与自己相关,然后我必须基于使用某些属性在两个实体中“共享”的属性创建一种动态关系。然后,我可以使用其中一个属性(在两个实体中具有相同的值)来选择与之相关的其他实体。
如果我想将所有数据存储在一个数据库中,那么我只会使我的数据库上下文从
IdentityDbContext
继承。但这并非如此,因为我想将两个数据集与彼此分开。并且关于修改身份用户 - Yiyi您提到的DOC中,您只需要创建一个可以从“ IdentityUser”继承的类,并在我们的用户中使用某些添加剂属性,在我们的数据库上下文中更改使用的用户(因此“ AddDbContext”)和所述数据库上下文类(修改继承),然后迁移和更新。
Nevermind, I've found needed informations in this answer (even though I searched before, I've just now found that whole post and answer) - using EF Core IdentityContext and DbContext both for order management
So basically - if I want to use seperate databases with entities that relate to themselves, then I have to create kind of dynamic relation based on using some property that will be "shared" among both of entities. Then I can use one of those properties (that will have the same value in both entities) to select those other entities that should be related with it.
If I would like to store all of my data in one database then I would just make my database context inherit from
IdentityDbContext
. But that wasn't the case here because I wanted to seperate both data sets from eachother.And concerning modifying Identity users - it's all in doc mentioned by Yiyi You, you just need to create a class that will inherit from "IdentityUser" with some additonal properties that we want in our users, change used user in our database context (so in "AddDbContext") and in said database context class (modify inheritance), then just migrate and update.