2014-10-12 76 views
5

我有这个设置与代码第一种模式:从IdentityUser访问导航属性时,惰性加载是关闭

public class TestContext :IdentityDbContext<TestUser> 
{ 
    public TestContext() 
     : base("TestConnection") 
    {   
     this.Configuration.LazyLoadingEnabled = false; 

    } 

    public DbSet<Customer> Customers{get;set;} 

} 

public class TestUser : IdentityUser 
{ 
    public virtual Customer Customer { get; set; } 
} 

public class Customer 
{ 
    public int Id { get; set; } 
    public string FirstName { get; set; } 
    public string LastName {get; set;} 
} 

我已经延长了IdentityUser包含“客户”类的一个实例。

现在考虑下面的代码:

var user = UserManager.FindById("some id");     
if (user != null) 
{  
    string str=user.Customer.FirstName; //since lazy loading is off, user.Customer is null and hence gives null reference exception. 
} 

因为延迟加载是关闭的,user.Customer为空,因此给空引用异常。 如果有人能够帮助我在LazyLoading关闭时访问IdentityUser的导航属性,我会很高兴。

谢谢。

+0

配置是TestUser的和客户之间的关系呢? – jd4u 2014-10-13 13:37:06

+0

我们是否需要这种关系的特殊配置?我很抱歉,但我不知道任何,请赐教。 – 2014-10-14 02:32:21

+0

回顾两件事:1.DbContext的OnModelCreating方法&2. LazyLoadingEnabled:http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/reading-related-data-with- the-entity-framework-in-an-asp-net-mvc-application – jd4u 2014-10-14 11:39:09

回答

8

如果您总是希望在不使用延迟加载的情况下加载相关数据,那么您需要编写自己的UserStore实现并将其插入UserManager。例如..

public class ApplicationUserStore : UserStore<TestUser> 
{ 
    public ApplicationUserStore(TestContext context) : base(context) 
    { 
    } 

    public override TestUser FindByIdAsync(string userId) 
    { 
     return Users.Include(c => c.Customer).FirstOrDefault(u => u.Id == userId); 
     //you may want to chain in some other .Include()s like Roles, Claims, Logins etc.. 
    } 
} 

那么当您创建的UserManager,插件本实施UserStore的,和你的Customer数据将与用户加载..这可能看起来像..

public class TestUserManager : UserManager<TestUser> 
{ 
    public TestUserManager() : base(new ApplicationUserStore(new TestContext())) 
    { 
    } 

} 

取决于您的项目,UserManager实施将有所不同。

+1

你先生已经救了我一天。我意识到急切的加载不能用于IdentityDbContext,所以这是解决方法。 – hbulens 2015-09-08 13:44:14

4

我有这个解决:

创建自定义的UserManager

public class ApplicationUserManager : UserManager<ApplicationUser> 
{ 
    public ApplicationUserManager(IUserStore<ApplicationUser> store, IOptions<IdentityOptions> optionsAccessor, IPasswordHasher<ApplicationUser> passwordHasher, IEnumerable<IUserValidator<ApplicationUser>> userValidators, IEnumerable<IPasswordValidator<ApplicationUser>> passwordValidators, ILookupNormalizer keyNormalizer, IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<ApplicationUser>> logger, IHttpContextAccessor contextAccessor) : base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger, contextAccessor) { } 

    public override Task<ApplicationUser> FindByIdAsync(string userId) 
    { 
     return Users.Include(c => c.Esercizio).FirstOrDefaultAsync(u => u.Id == userId); 
    } 
} 

替换默认的UserManager服务

在你ConfigureServices补充一点:

services.AddIdentity<ApplicationUser, IdentityRole>().AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders().AddUserManager<ApplicationUserManager>(); 

为DI

更改的参数从

[FromServices]UserManager<ApplicationUser> userManager 

[FromServices]ApplicationUserManager userManager 

我希望这有助于

+0

谢谢你一百万!我几乎都放弃了这样做的方式,并打算做另一个数据库调用来拉取必要的对象,因为我能找到的所有解决方案都涉及覆盖几乎整个框架! – 2017-10-25 07:39:44