【问题标题】:Entity Framework Code Only Relationship Not Being Read实体框架代码仅关系未读取
【发布时间】:2011-03-07 08:17:42
【问题描述】:

(这似乎是一个很长的问题,但实际上并不诚实!)

我正在尝试使用 Entity Framework 4 和 CTP 3 版本的 Code Only 进行简单的概念证明。感觉就像我错过了一些非常明显和简单的东西。

我有以下测试失败:

[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...
}

child 的读取工作正常,我得到了 ChildTable,其 ParentTableId 的值是“1”,正如我所料,但 ParentTable 属性为 NULL。我不希望出现这种情况,因为我的 POCO 具有所有虚拟属性(见下文),并且 EF4 默认启用了延迟加载。

我错过了什么?


数据库

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 实体

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; }
}

实体配置

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);
    }
}

(感谢您阅读本文!)

【问题讨论】:

    标签: entity-framework entity-framework-4 poco code-first


    【解决方案1】:

    据了解,您只是不加载此导航属性。

    这将导致预加载。

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

    或者你可以通过设置ctx.ContextOptions.LazyLoadingEnabled = true;来启用延迟加载

    【讨论】:

      猜你喜欢
      • 2011-03-08
      • 2012-10-11
      • 2017-07-05
      • 2012-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多