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