.Include Forces EF to throw sequence不包含任何元素

时间:2011-01-25 00:36:13

标签: entity-framework-4 code-first

现有SQL Server表

两次测试:

称之为A:

    [Test]
    public void AssertAccessPolicyWithIdAndChecksum()
    {
        var pol = Repo.GetPolicyFlightStatus(aut_id: 44544, checksum: "QXSDENY");
        Assert.NotNull(pol);
    }

称之为B

    [Test]
    public void AssertGetFriendlyPolicy()
    {
        var lineRepo = new tbl_StatusRepository();
        var pol = Repo.GetPolicyFlightStatus(aut_id: 44544, checksum: "QXSDENY");
        Assert.AreEqual("With Underwriter", pol.tbl_Status.txt_friendlyName);
        Assert.AreEqual("WC/Longshore", pol.tbl_Line.txt_friendlyName);
    }

模特:

public partial class tbl_Policy
{
    [Key]
    public int aut_id { get; set; }
    [ForeignKey("tbl_Status")]
    public int int_statusID { get; set; }
    public virtual tbl_Status tbl_Status { get; set; }
    [ForeignKey("tbl_Line")]
    public int int_lineID { get; set; }
    public virtual tbl_Line tbl_Line { get; set; }
}

public class tbl_Status
{
    [Key]
    public int aut_id { get; set; }
    public string txt_status { get; set; }
    public string txt_friendlyName { get; set; }
    public virtual tbl_Policy tbl_Policy { get; set; }
}

public class tbl_Line
{
    [Key]
    public int aut_id { get; set; }
    public string txt_Line { get; set; }
    public string txt_friendlyName { get; set; }
    public virtual tbl_Policy tbl_Policy { get; set; }
}

运行时

      internal static tbl_Policy GetPolicyFlightStatus(int aut_id, string checksum)
    {
        if (Transcoder.Transcode(aut_id) == checksum)
        {
            var ctx = new LIGDataContext();
            return ctx.tbl_Policy.Include("tbl_Line").Include("tbl_Status").Single(f => f.aut_id == aut_id);
        }
        return null;
    }

TestA通过 TestB在第一个断言行抛出异常


为SubTables添加包含

 internal static tbl_Policy GetPolicyFlightStatus(int aut_id, string checksum)
    {
        if (Transcoder.Transcode(aut_id) == checksum)
        {
            var ctx = new LIGDataContext();
            return ctx.tbl_Policy.Include("tbl_Line").Include("tbl_Status").Single(f => f.aut_id == aut_id);
        }
        return null;
    }

测试A和测试B投掷 LIG2010RedesignMVC3.LIGMVC2010FlightTrackerTests.AssertAccessPolicyWithIdAndChecksum: System.InvalidOperationException:Sequence不包含任何元素

<小时/> LIG2010RedesignMVC3.LIGMVC2010FlightTrackerTests.AssertGetFriendlyPolicy: System.InvalidOperationException:Sequence不包含元素

2 个答案:

答案 0 :(得分:1)

对象模型中存在一些问题。基本上,您尝试与外键建立1:1关联,而代码首先不支持此方案。因此,它将您的所有关联转换为共享主键关联,并且tbl_Policy上的任何外键都不会成为数据库中的外键。首先,您需要修复模型,因为这可能会在运行时导致一堆异常。

目前,有两种方法可以在Code-First中映射1:1关联:
1. Shared Primary Key Associations
2. One-to-One Foreign Key Associations

查看哪个更好地描述了您的域模型,我可以更改您的对象模型以匹配该模型。

答案 1 :(得分:1)

我还需要重新设计我的模型(归功于Morteza,但我想在这里记录我的记录

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<tbl_Line>()
            .HasMany(d => d.tbl_Policy)
            .WithRequired(c => c.tbl_Line)
            .HasForeignKey(c => c.int_lineID);
        modelBuilder.Entity<tbl_Status>()
            .HasMany(d => d.tbl_Policy)
            .WithRequired(c => c.tbl_Status)
            .HasForeignKey(c => c.int_statusID);
    }