locked
Accessing Navigation Properties from IdentityUser when LazyLoading is off RRS feed

  • Question

  • I've this setup with code first model:

    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;}
    }

    I've extended the IdentityUser to contain an instance of "Customer" Class.

    Now consider this code:

    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.
    }

    since lazy loading is off, user.Customer is null and hence gives null reference exception. I'll be glad if anyone can help me in accessing the Navigation Properties of IdentityUser when LazyLoading is off.

    Thanks.

    • Moved by CoolDadTx Monday, October 13, 2014 4:02 PM EF related
    Sunday, October 12, 2014 11:28 AM

Answers

  • Well, if the Users collection is not part of the context you will have to load the Customer entity from the context and manually assign the Customer property of your TestUser class in your UserManager.FindById method:

    public TestUser FindById(string id)
    {
    var user = get user as before....;
    
    user.Customer = context.Customers.FirstOrDefault(c => c.UserId = user.Id);
    
    return user;
    }
    

    Entity Framework won't be able to this for you.

    • Proposed as answer by Fred Bao Thursday, October 16, 2014 5:18 AM
    • Marked as answer by Fred Bao Monday, October 20, 2014 3:44 AM
    Sunday, October 12, 2014 12:37 PM

All replies

  • You must eagerly loading or explicitly loading the related entities as described on MSDN here: http://msdn.microsoft.com/en-us/data/jj574232.aspx

    Something like this:

        var user = yourContext.Users
                            .Where(u => u.Id == "some id...") 
                            .Include("Customer") 
                            .FirstOrDefault(); 
    

    But please ask Entity Framework questions in the following forum: https://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework

    Please also remember to mark helpful posts as answer and/or helpful.

    Sunday, October 12, 2014 11:39 AM
  • Thanks for your help.

    I am sorry but I cannot use Eagerly Loading as I am using ASP.Net Identity for authentication and the "Users" collection is not available to me as a part of my db context.

    Can we migrate this question to this forum: https://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework ?


    • Edited by AmitMittal Sunday, October 12, 2014 12:32 PM
    Sunday, October 12, 2014 12:32 PM
  • Well, if the Users collection is not part of the context you will have to load the Customer entity from the context and manually assign the Customer property of your TestUser class in your UserManager.FindById method:

    public TestUser FindById(string id)
    {
    var user = get user as before....;
    
    user.Customer = context.Customers.FirstOrDefault(c => c.UserId = user.Id);
    
    return user;
    }
    

    Entity Framework won't be able to this for you.

    • Proposed as answer by Fred Bao Thursday, October 16, 2014 5:18 AM
    • Marked as answer by Fred Bao Monday, October 20, 2014 3:44 AM
    Sunday, October 12, 2014 12:37 PM