How to get data from a related table Entity Framework?

There are the following databases
29a3e091375c4256810a9bbae420bed1.png
you need to obtain the following data {bid.id bid.text bid.datetime_created, user_created_name, user_executor_name}
doing so
dbContext = new Model1(); database model CodeFirst
dbContext.BID.Load();
var bid = dbContext.BID.Local;
bindingSource1.DataSource = bid.ToBindingList();
dataGridView1.DataSource = bindingSource1;


All displays, but instead of data from a connected table "System.Data.Entity.DynamicProxies. ..." , as I understand it is because of LazyLoad.
If you disable LazyLoad so
dbContext.Configuration.LazyLoadingEnabled = false;
then nothing is displayed.

Tell me what to write to data loaded from linked tables ?
July 2nd 19 at 18:20
4 answers
July 2nd 19 at 18:22
Approximately so, it is not clear what the context.
using(var db = new Model1())
{
 var data = (from bid in db.BID 
 join u_c in db.Users on u_c.id = bid.user_id_created
 join u_e in db.Users on u_e.id = bid.user_id_executor
 select new BIDDto
{
 id = bid.id 
 text = bid.text 
 datetime_created = bid.datetime_created, 
 user_created_name = u_c.Name 
 user_executor_name = u_e.Name
}).ToArray();
 /// Then put data where you want
}


BIDDto is a class with the fields you want.
July 2nd 19 at 18:24
Use Include()
July 2nd 19 at 18:26
A little strange, the code You gave. And you can see the code for Your dbContext?

If You use codfirst, the tables are linked by the setting of the model through the creation of virtual properties in the model, indicating foreign keys and, if necessary, are configured with attributes or Fluent API. Then the data will be moved machine dbContext.Bid.UserCreated.Id
July 2nd 19 at 18:28
In your case, the code for the Code-First will look approximately so:
[Table("Role")]
public class Role
{
 [Column("id"), Key]
 public int Id { get; set; }

 [Column("name"), Required]
 public string Name { get; set; }
}

[Table("User")]
public class User
{
 [Column("id"), Key]
 public int Id { get; set; }

 [Column("name"), Required]
 public string Name { get; set; }

 [Column("password"), Required]
 public string Password { get; set; }

 [Column("role_id"), ForeignKey("Role")]
 public int RoleId { get; set; }

 public virtual Role Role { get; set; }

[InverseProperty("Creator")]
 public virtual List<bid> BidsWhereUserAsCreator { get; set; }

[InverseProperty("Executor")]
 public virtual List<bid> BidsWhereUserAsExecutor { get; set; }
}

[Table("bid")]
public class Bid
{
 [Column("id"), Key]
 public int Id { get; set; }

 [Column("Text"), Required]
 public string Text { get; set; }

[Column("DateTime_Created")]
 public DateTime Created { get; set; }

 [Column("user_id_created"), ForeignKey("Creator")]
 public int CreatorId { get; set; }

 [Column("user_id_executor"), ForeignKey("Executor")]
 public int ExecutorId { get; set; }

 public virtual User Creator { get; set; }

 public virtual User Executor { get; set; }
}

public class MyDbContext : DbContext
{
 public virtual DbSet<role> Roles { get; set; }

 public virtual DbSet<user> Users { get; set; }

 public virtual DbSet<bid> Bids { get; set; }

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
 // remove the cascade delete
 // http://stackoverflow.com/questions/17127351/introducing-foreign-key-constraint-may-cause-cycles-or-multiple-cascade-paths
modelBuilder.Conventions.Remove<onetomanycascadedeleteconvention>();
modelBuilder.Conventions.Remove<manytomanycascadedeleteconvention>();
}

 public static void Test()
{
 using (MyDbContext dbContext = new MyDbContext())
{
 // say that it is not necessary to create a dynamically generated proxy classes
 // (which the System.Data.Entity.DynamicProxies...)
 dbContext.Configuration.ProxyCreationEnabled = false;
 // disable lazy loading
 dbContext.Configuration.LazyLoadingEnabled = false;

 // class fields are filled, but these navigation properties are not loaded.
 List<bid> bidsOnly = dbContext.Bids.ToList();

 // fields of navigation properties can be loaded through Include
 List<bid> bidsWithAllInfo = dbContext.Bids.Include(b => b.Creator).Include(b => b.Executor).Include(b => b.Creator.Role).Include(b => b.Executor.Role).ToList();

 // data for navigation field Creator will be downloaded automatically
 IEnumerable<bid> bids = dbContext.Bids.Include(b => b.Executor).Where(b => b.Creator.RoleId == 1).ToList().Select(
 b => new Bid()
{
 Id = b.Id
 Text = b.Text
 Created = b.Created
 Creator = new User() { Name = b.Creator.Name}
 Executor = new User() { Name = b.Executor.Name }
});
}
}
}</bid></bid></bid></manytomanycascadedeleteconvention></onetomanycascadedeleteconvention></bid></user></role></bid></bid>


In the case you described no lazy loading or proxy classes do not interfere with to get the desired result and the configuration of the default can not be changed.

Find more questions by tags C#Entity Framework