This question has already been answered:

I am writing a chat authorization using the Entity Framework. Everything has already been written in general for a long time, but when I began to carefully test, I found a bug. The essence of the user looks like this:

public class UserInfo { [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] public long Id { get; set; } [Required, MinLength(4), MaxLength(32)] public string Username { get; set; } [Required, MinLength(4), MaxLength(32)] public string Password { get; set; } public long? RoleId { get; set; } [ForeignKey(nameof(RoleId))] public RoleInfo Role { get; set; } public long? BanId { get; set; } [ForeignKey(nameof(BanId))] public BanInfo Ban { get; set; } } 

So, during registration, UserInfo and RoleInfo are created, Role is assigned, the entity is added to the context list, and SaveChanges is called. But then authorization only works until you stop the server. When restarting the server (and accordingly loading the database), Role disappears from the entity. Although there in the debugger you can see that RoleId is specified, but the essence of Role itself remains null. Why it happens? ForeignKey specified ...

UPD. container code:

 public class DbContainer : DbContext { public DbContainer() : base($"name={nameof(DbContainer)}") { } public virtual DbSet<UserInfo> Users { get; set; } public virtual DbSet<RoleInfo> Roles { get; set; } public virtual DbSet<BanInfo> Bans { get; set; } } 

Reported as a duplicate by members aleksandr barakin , rdorn , cheops , Streletz , Grundy May 27, '16 at 10:43 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • @Bald Ie as this duplicate is your question was. O_o - PECHAILTER
  • Duplicate within this resource and not from a single user, in your case your question boils down to: How to fill a property with a value from a related table. which there is an answer in my question / answer - Bald
  • Did you manage to fill the Role property? - Bald
  • @Bald Yes, thanks. - PECHAPTER

1 answer 1

If navigation links are used, then you can use lazy loading, for this the necessary property must be declared using the virtual modifier, then the first access to the property will make a query to the database, which fills the Role property Include(x=>x.Role) or explicit userInfo(x=>x.Role).Load() download

a little more detail can be found here

PS solution 100% works with the code first approach, it may differ for other approaches

  • I just can't understand what the Include function does and where to insert it. But this userInfo (x => x.Role) .Load () generally looks very strange. How did you manage to make a variable call a function like a function? Or what is it? Yes, code is used first. - PECHAPTER
  • one
    Include() loads the Role value from a related table. it should be connected like this: context.UserInfo.Where(...).Include(x=>x.Role) - Bald
  • But judging by the fact that I read through the link given by you, I use lazy loading and Include is not needed. So why doesn't it work? - PECHAPTER
  • one
    @DarkByte public RoleInfo Role { get; set; } public RoleInfo Role { get; set; } public RoleInfo Role { get; set; } judging by this, you do not use lazy loading - there is no virtual modifier, i.e. should be public virtual RoleInfo Role { get; set; } public virtual RoleInfo Role { get; set; } public virtual RoleInfo Role { get; set; } - Bald
  • Oh, that's it. I thought the container should be virtual, but it also turns out to be in entities. I'm going to try now. - PECHAPTER