I'm using EF Code First with a pre-existing database.
Two objects:
public Foo
{
public int FooId {get;set;}
}
public Bar
{
public int BarId {get;set;}
public virtual Foo Foo {get;set;}
}
Both FooId and BarId are primary keys in the database, and the Bar table has a column FooId which is a foreign key pointing at the Foo table.
When I select a Bar, Foo is a null reference. I would have thought EF would have automatically pulled the two of them together, but perhaps I'm missing something?
Database mappings:
public class EFCodeFirst : DbContext
{
public EFCodeFirst()
{
this.Database.Connection.ConnectionString = ConfigurationManager.ConnectionStrings["AlternateString"].ConnectionString;
}
public DBSet<Foo> Foos {get;set;}
public DBSet<Bar> Bars {get;set;}
protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
{
modelBuilder.IncludeMetadataInDatabase = false;
modelBuilder.Entity<Foo>().MapSingleType().ToTable("Foo");
modelBuilder.Entity<Bar>().MapSingleType().ToTable("Bar");
}
}
Include("Foo")it works.