0

I'm working on a website using Asp.Net MVC 5 and Entity Framework 6. I'm trying to add a One-To-One relationship between ApplicaitonUser and UserBalance entities. Here's the 2 classes:

public class ApplicationUser : IdentityUser
{
    public virtual UserBalance Balance { get; set; }
}

public class UserBalance
{
    [Key]
    public int Id { get; set; }

    public decimal Balance { get; set; }

    [Required]
    public virtual ApplicationUser User { get; set; }

    [Timestamp]
    public byte[] RowVersion { get; set; }
}

When I load the users from database the Entity Framework loads an empty UserBalance and I can't save the changes. I have to use .Include("Balance") to load the information.

How can I make the Entity Framework automatically load the balance information from the database without the need of the Include function?

Update:

This is how I retrieve the user which returns the user without the Balance.

var id = User.Identity.GetUserId();
var user = await db.Users.FirstOrDefaultAsync(u => u.Id == id);

This is how I retrieve it with the Balance:

var id = User.Identity.GetUserId();
var user = await db.Users.Include("Balance").FirstOrDefaultAsync(u => u.Id == userid);
Community
  • 1
  • 1
Alireza Noori
  • 14,961
  • 30
  • 95
  • 179
  • Could you show how you retrieve the data? – Win Apr 05 '17 at 12:48
  • See [here](http://stackoverflow.com/questions/18917423/entity-framework-eager-load-not-returning-data-lazy-load-does). – Steve Greene Apr 05 '17 at 13:25
  • @Win see the update. – Alireza Noori Apr 05 '17 at 14:51
  • Where have you configured the relationship? I don't see any annotations nor ModelBuilder code? – Erik Philips Apr 05 '17 at 14:53
  • @SteveGreene I checked the link. The lazy loading is enabled for my context. I use it for other parts of even the same entity (`ApplicationUser`). The `virtual` modifier is there too. Not sure if I'm missing something. – Alireza Noori Apr 05 '17 at 14:53
  • @ErikPhilips EF should know I'm looking for a 1-to-1-or-0 relationship because there's a `RequiredAttribute` for the `ApplicationUser` of `UserBalance`. Either way I tried adding the code explicitly with the fluent API. It didn't help. – Alireza Noori Apr 05 '17 at 14:55
  • If you don't configure it, it doesn't just magically know the relationship... – Erik Philips Apr 05 '17 at 15:00
  • Actually, EF can establish relationships via [convention](https://www.safaribooksonline.com/library/view/programming-entity-framework/9781449317867/ch04.html). – Steve Greene Apr 05 '17 at 15:04
  • 1
    Yes, you have [lazy loading](https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx) properly configured, but you need the Include() regardless as Gert points out. – Steve Greene Apr 05 '17 at 15:07
  • @SteveGreene but why does it work for some entities and not for others? Also I used to retrieve the data using `Include` but I want to use `userManager.SetLockoutEnabledAsync` and it throws an exception because of this. – Alireza Noori Apr 06 '17 at 11:35

0 Answers0