包括ASP.Net 2.0的身份和UserManager.Users.ToListAsync财产UserManager.FindByIdAsync [英] Include property on ASP.Net Identity 2.0 UserManager.Users.ToListAsync and UserManager.FindByIdAsync
问题描述
我想实现Asp.Net 2.0身份。我曾经的帮助下<托管的迄今为止非常好href=\"http://typecastexception.com/post/2014/06/22/ASPNET-Identity-20-Customizing-Users-and-Roles.aspx\"相对=nofollow称号=本博客>这个博客。但我去一个稍微不同的道路。我想一些数据不被用户对象的一部分,而是一个新创建的Customer对象。我想保持的身份验证和授权数据从我的业务数据分开。
I am trying to implement Asp.Net Identity 2.0. I have managed so far very well with the help of this blog. But I went a slightly different road. I want some data not to be part of the User object, but rather of a newly created Customer object. I want to keep the authentication and authorization data separate from my business data.
所以在我的情况下,我加入了客户财产修改ApplicationUser类:
So in my case I modified the ApplicationUser class by adding a customer property:
public Customer Customer { get; set; }
Customer类看起来是这样的:
The Customer class looks like this:
public class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Insertion { get; set; }
public Gender Gender { get; set; }
public DateTime Birthdate { get; set; }
...etc.
public virtual ApplicationUser User { get; set; }
}
我还增加了德关系的ApplicationDbContext:
I also added the relationship in de the ApplicationDbContext:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>()
.HasRequired(u => u.User);
base.OnModelCreating(modelBuilder);
}
所以,现在一些修改寄存器视图模型,我能够注册为用户,并同时拥有在AspNetUsers表中创建一个用户记录,并在客户表中的客户记录与该用户记录的引用控制器后。到目前为止好。
So now after some modifications to the register viewmodel and the controller I am able to register as a user and have both a User record created in the AspNetUsers table and a Customer record in the Customer table with a reference to the User record. So far so good.
但现在当我检索例如,用户对于显示目的客户属性为空。这是有道理的,因为没有加载基础记录。通常我会用.INCLUDE关键字加载相关记录。但我不知道如何使用随Asp.Net 2.0标识默认的UserManager时做的。
But now when I retrieve the user for example for display purposes the Customer property is empty. Which makes sense, since the underlying record is not loaded. Normally I would use the .Include keyword to load the related records. But I don't know how to do that when using the default UserManager that comes with Asp.Net identity 2.0.
有谁知道如何实现这一目标?
Does anybody know how to achieve this?
推荐答案
我找到了答案。 Probablu需要回答之前更多的搜索。答案是基于这个答案的 SO问题
I found the answer. Probablu need more searching before answering. The answer is based on the answer in this SO Question
我添加了一个名为ApplicationUserStore类。
I have added a class called ApplicationUserStore.
public class ApplicationUserStore : UserStore<ApplicationUser>
{
public ApplicationUserStore(DbContext context)
: base(context)
{
}
public override System.Threading.Tasks.Task<ApplicationUser> FindByIdAsync(string userId)
{
return Users.Include(u=>u.Customer).Include(u => u.Roles).Include(u => u.Claims).Include(u => u.Logins).FirstOrDefaultAsync(u => u.Id == userId);
}
}
在这个类我已经重写了FindByIdAsync包括客户记录。
In this class I have overriden the FindByIdAsync to include the customer record.
然后我IdentityConfig.cs的方法创建,使其通过新创建的ApplicationUserStore代替UserStore的返回的ApplicationUserManager改变了第一道防线。
Then I changed in IdentityConfig.cs the first line in the method Create which returns the ApplicationUserManager so that it passes the newly created ApplicationUserStore instead of the UserStore.
var manager = new ApplicationUserManager(new ApplicationUserStore(context.Get<ApplicationDbContext>()));
我大概也有覆盖其他一些获取方法,包括客户记录。
I probably also have to override some other fetching methods to include the customer records.
这篇关于包括ASP.Net 2.0的身份和UserManager.Users.ToListAsync财产UserManager.FindByIdAsync的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!