This is the first time I am making a project in C# with the Entity Framework 4.3.1. I am having troubles with getting all the data from the Tournament table, the Address to be precise.
First of all, here is my Code First code. When I run this, the 2 tables are created correctly with the correct relation. I have a lot more columns defined in those classes, but for this example I only show a few.
public class EFDbContext : DbContext
{
public EFDbContext()
: base("ApplicationServices")
{
}
public DbSet<Tournament> tournaments { get; set; }
public DbSet<Address> addresses { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
public class Tournament
{
[ScaffoldColumn(false)]
public int TournamentId { get; set; }
[Required(ErrorMessage="Tournament name is a required field")]
public string Name { get; set; }
[Required(ErrorMessage="Address is a required field")]
public Address Address { get; set; }
}
public class Address
{
[ScaffoldColumn(false)]
public int AddressId { get; set; }
[Required(ErrorMessage="Street name is a required field")]
public string Street { get; set; }
[Required(ErrorMessage="House number is a required field")]
public string HouseNo { get; set; }
}
}
When I insert a new Tournament with an Address and I go check in the database if the relation is used I can see it worked. The tournament has an Address_AddressId value pointing to the new inserted Address. But when I try to get the info by doing this:
Tournament tournament = context.tournaments.Find(id);
and I debug it, I can see all the data from Tournament is in the tournament object, except for the Address. This is set to null and I have absolutely no idea why.
Can you guys help me out?
Thanks in advance,
Bart
You need to get familiar a bit how you can load related data with Entity Framework. An introduction is here: http://blogs.msdn.com/b/adonet/archive/2011/01/31/using-dbcontext-in-ef-feature-ctp5-part-6-loading-related-entities.aspx These are really basics you need to know to work efficiently with Entity Framework.
The behaviour you are seeing is expected. Entity Framework doesn’t load a navigation property – like
Tournament.Address– automatically when you just load the parent entityTournament(withFindin your example).There are basically three options to load related data:
Eager loading:
Tournament and address will we loaded in a single roundtrip and database query.
Lazy loading: Mark your navigation property as
virtual:EF will dynamically create a proxy object (derived from
Tournament) when you load the tournament which is capable to load the related entity as soon as you access one of its properties:Explicit loading:
This is similar to lazy loading because you also need two queries and roundtrips to the database, but you control explicitly when the
Addressis loaded. Explicit loading has the option to filter or sort the related data (in case of navigation collections) which the other two options do not have.