Question

(This looks like a long question, but it's not really, honest!)

I am trying to get a simple proof of concept working with Entity Framework 4 and the CTP 3 version of Code Only. It feels like I'm missing something really obvious and simple.

I have this following test which is failing:

[TestFixture]
public class ParentChildTests
{
    [Test]
    public void ChildRead_DatabaseContainsRelatedObjects_ParentIsNotNull()
    {
        var ctx = GetMyObjectContext();
        var child = ctx.Children.Where(c => c.Id == 1).Single();
        var parent = child.ParentTable;
        Assert.That(parent, Is.Not.Null);
    }

    // GetMyObjectContext etc...
}

The read of child works fine and I get back a ChildTable whose ParentTableId value is '1' as I would expect, but the ParentTable property is NULL. I do not expect this because my POCOs have all virtual properties (see below) and EF4 has lazy loading enabled by default.

What am I missing?


Database

create table parent_table
(
    parent_table_id   int identity(1,1) primary key,
    parent_table_name varchar(50) not null,
    display_name      varchar(50)
)

create table child_table
(
    child_table_id   int identity(1,1) primary key,
    child_table_name varchar(50) not null,
    parent_table_id  int not null
)

alter table child_table add constraint FK_child_table__parent_table
foreign key (parent_table_id) references parent_table(parent_table_id)

POCO Entities

public class ParentTable
{
    public virtual int    Id          { get; set; }
    public virtual string Name        { get; set; }
    public virtual string DisplayName { get; set; }
}

public class ChildTable
{
    public virtual int         Id            { get; set; }
    public virtual string      Name          { get; set; }
    public virtual int         ParentTableId { get; set; }
    public virtual ParentTable ParentTable   { get; set; }
}

Entity Configurations

public class ParentTableConfiguration : EntityConfiguration<ParentTable>
{
    public ParentTableConfiguration()
    {
        MapSingleType(pt => new
        {
            parent_table_id   = pt.Id,
            parent_table_name = pt.Name,
            display_name      = pt.DisplayName,
        })
        .ToTable("dbo.parent_table");

        Property( pt => pt.Id   ).IsIdentity();
        Property( pt => pt.Name ).IsRequired();
    }
}

public class ChildTableConfiguration : EntityConfiguration<ChildTable>
{
    public ChildTableConfiguration()
    {
        MapSingleType(ct => new
        {
            child_table_id   = ct.Id,
            child_table_name = ct.Name,
            parent_table_id  = ct.ParentTableId,
        })
        .ToTable("dbo.child_table");

        Property( ct => ct.Id   ).IsIdentity();
        Property( ct => ct.Name ).IsRequired();

        Relationship(ct => ct.ParentTable)
            .HasConstraint((ct, pt) => ct.ParentTableId == pt.Id);
    }
}

(Thanks for reading this far!)

Was it helpful?

Solution

As far as understand you just do not load this navigation property.

This will result in eager loading.

var child = ctx.Children.Include("ParentTable").Where(c => c.Id == 1).Single();

Or you could enable lazy loading by setting ctx.ContextOptions.LazyLoadingEnabled = true;

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top