简体   繁体   English

使用EF7,MVC6,ASP.NET Identity保存和检索CreatedBy用户信息

[英]Saving and retrieving CreatedBy User information with EF7, MVC6, ASP.NET Identity

I am using the latest MVC6 and Entity Framework 7, however I am sure many techniques used in MVC5 and Entity Framework 6 could help answer my question also. 我正在使用最新的MVC6和Entity Framework 7,但是我确信MVC5和Entity Framework 6中使用的许多技术也可以帮助回答我的问题。

Almost all of the tables in my database have the following 4 fields for auditing: CreatedDate, CreatedBy, ModifiedDate, ModifiedBy. 数据库中的几乎所有表都具有以下4个字段用于审核:CreatedDate,CreatedBy,ModifiedDate,ModifiedBy。

I am trying to figure out which field from the built in IdentityUser (AspNetUsers table) I should be storing in the CreatedBy field when saving items to the database. 我试图从内置IdentityUser(AspNetUsers表)中找出哪个字段,将项目保存到数据库时应该存储在CreatedBy字段中。

I started by trying to use 'Username' since it is easily accessible by calling User.Identity.Name and passing it down to the repository when saving. 我首先尝试使用“用户名”,因为可以通过调用User.Identity.Name并在保存时将其传递到存储库来轻松访问它。 Here is how I configured EF using Fluent API to help with retrieving the User who created an item along with all of their fields: 这是我使用Fluent API配置EF的方法,以帮助检索创建项目的用户及其所有字段:

builder.Entity<BlogPost>()
    .Property(bp => bp.CreatedBy)
    .HasMaxLength(256);

builder.Entity<BlogPost>()
    .HasOne(bp => bp.CreatedByUser)
    .WithMany()
    .HasForeignKey(bp => bp.CreatedBy)
    .HasPrincipalKey(u => u.UserName);

But then I noticed that Entity Framework yells when trying to add migrations and create the database because Username needs to be set as a primary key or it can't be used as a foreign key in another table. 但是后来我注意到,在尝试添加迁移和创建数据库时,Entity Framework会大喊大叫,因为需要将Username设置为主键,或者不能将Username用作另一个表中的外键。

Then I got to the point where I figured I would just use the actual Id which is a GUID. 然后我发现我只使用实际的ID(即GUID)。 The problem with this technique is that the Id of the current logged in user isn't easily available when trying to save: 'User.Identity.Name' is all that is there. 这项技术的问题在于,尝试保存时,当前登录用户的ID不太容易使用:仅提供“ User.Identity.Name”。

Here are a couple of questions that I would like someone with EF and Auditing experience to try and answer for me: 我希望拥有EF和审计经验的人为我解答以下几个问题:

  1. Do people even use Audit fields in all of their tables anymore? 人们甚至不再在所有表中使用“审核”字段吗? I don't see many others asking questions about this and Microsoft definitely doesn't make it easy to work with their new Identity system and custom audit fields. 我看不到其他许多人对此提出疑问,并且Microsoft绝对不容易使用其新的Identity系统和自定义审核字段。

  2. Should I be storing Username or Id in my CreatedBy field. 我应该在CreatedBy字段中存储用户名还是ID。 Some say this might be preference but I really want to know what direction Microsoft might be pushing with the new Identity. 有人说这可能是首选,但我真的很想知道微软使用新的Identity可能会朝哪个方向发展。 The problem with storing Id is that it is hard to get it when saving and the problem with storing Username is that it isn't a primary key in AspNetUsers table. 存储Id的问题是保存时很难获取它,存储用户名的问题在于它不是AspNetUsers表中的主键。

  3. I really would just like to know of a good pattern in general when using EF that handles auditing when saving, and retrieving the User and setting it as a Navigation property on my entities that need it when pulling records from the database. 我真的很想知道使用EF时一般的良好模式,它可以在保存时处理审核,并在从数据库中提取记录时检索User并将其设置为需要它的我的实体上的Navigation属性。

I am trying to figure out which field from the built in IdentityUser (AspNetUsers table) I should be storing in the CreatedBy field when saving items to the database. 我试图从内置IdentityUser(AspNetUsers表)中找出哪个字段,将项目保存到数据库时应该存储在CreatedBy字段中。

The user name of the person using the site (on Thread.CurrentPrincipal , accessed in ASP.Net via User.Identity.Name ). 使用该站点的人员的用户名(在Thread.CurrentPrincipal ,可通过User.Identity.Name在ASP.Net中访问)。 Who else? 还有谁? That is the identity of the current authenticated user using your site, and it is what you should be putting in the audit tables. 使用您的站点的当前经过身份验证的用户的身份,这是您应放入审核表中的内容。

Do people even use Audit fields in all of their tables anymore? 人们甚至不再在所有表中使用“审核”字段吗? I don't see many others asking questions about this and Microsoft definitely doesn't make it easy to work with their new Identity system and custom audit fields. 我看不到其他许多人对此提出疑问,并且Microsoft绝对不容易使用其新的Identity系统和自定义审核字段。

Yes , people do! 是的 ,人们愿意! All the time! 每时每刻! If you are storing data that can be edited in any way by an end user (whether they are in your company or not), audit it. 如果您存储的数据可以由最终用户以任何方式编辑(无论它们是否在公司中),请对其进行审核。 Always. 总是。 I was going to say that all enterprises audit stuff to the extreme (and they do), but I even do that on my own personal projects. 我要说的是,所有企业都将审计内容做到极致(而且确实如此),但是我什至在自己的个人项目中也这样做。 Metrics are extremely important! 指标非常重要!

And one important thing to remember is that just because people aren't asking about it on StackOverflow or some other site doesn't mean that it isn't prevalent and critical in our industry . 需要记住的一件事是,仅仅因为人们没有在StackOverflow或其他网站上询问它, 并不意味着它在我们的行业中并不普遍且不重要

Should I be storing Username or Id in my CreatedBy field. 我应该在CreatedBy字段中存储用户名还是ID。 Some say this might be preference but I really want to know what direction Microsoft might be pushing with the new Identity 有人说这可能是首选,但我真的很想知道微软使用新的Identity可能会朝哪个方向发展

Microsoft (and the team behind their Identity framework) are doing a great job with providing us a secure and robust security framework. Microsoft(及其身份框架背后的团队)在为我们提供安全可靠的安全框架方面做得很好。 Maybe they would recommend their approach to this problem, but their framework isn't really meant to address those nuances (which can and will differ from system to system). 也许他们会建议他们解决这个问题的方法,但是他们的框架并不是真正要解决这些细微差别(不同系统之间可能会有所不同)。 At the end of the day, pick whichever suits the schema of your database. 在一天结束时,选择适合您数据库模式的任何一种。 I think most of the time that the Username would be appropriate to store (if it is unique within your system). 我认为大多数时候该用户名都适合存储(如果它在系统中是唯一的)。 After all, they both represent the same information (unless your usernames are not unique, which begs further questions). 毕竟,它们都代表相同的信息(除非您的用户名不是唯一的,这会引起进一步的问题)。

I really would just like to know of a good pattern in general when using EF that handles auditing when saving, and retrieving the User and setting it as a navigation property on my entities that need it when pulling records from the database. 我真的很想知道使用EF时一般的良好模式,它可以在保存时处理审核,并在从数据库中提取记录时检索User并将其设置为需要它的我的实体上的导航属性。

It is not, and most likely never will be EF's concern to help you with something like this. 不是,而且EF很可能永远不会为您提供类似帮助。 Sorry, that's just the way it is. 抱歉,就是这样。 Each application is unique, and EF (or any other ORM) can't be expected to meet everyone's needs. 每个应用程序都是唯一的,并且EF(或任何其他ORM)不能满足每个人的需求。

I realize all of this doesn't really provide you with concrete answers, but I had to drop some advice. 我意识到所有这些并不能真正为您提供具体答案,但是我不得不放弃一些建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM