0

这是我的模型的片段:

public class ContractParent
{
    public int ContractParentID { get; set; }
    public string Name { get; set; }
    public string Verbiage { get; set; }
    public int Position { get; set; }
    public bool IncludedByDefault { get; set; }
    public virtual IEnumerable<ContractChild> ContractChilds { get; set; }
    public virtual bool HasOnlyOneChild { get; set; }
}

public class ContractParentItem
{
    public int ContractParentItemID { get; set; }
    public virtual int ContractParentID { get; set; }
    public virtual ContractParent ContractParent { get; set; }
    public int ChargeID { get; set; }
    public virtual Charge Charge { get; set; }
}


public class Charge
{
    public int ChargeID { get; set; }         
    public virtual ChargeType ChargeType { get; set; }
    public int ChargeTypeID { get; set; }
    [Required]
    public string Name { get; set; }
    public virtual ICollection<ChargeContract> ChargeContracts { get; set; }
    public virtual Company Company { get; set; }
    public virtual int? CompanyID { get; set; }
    public virtual IEnumerable<ContractParentItem> ContractParentItems { get; set; }
    public virtual IEnumerable<ContractChildItem> ContractChildItems { get; set; }
}

当我尝试使用ChargeID = 687 访问 Charge 时:

Charge.ContractParentItem 为空。

但它不是!

在 SQL 中查看我的表ContractParentItem

有一个实体: ContractParentItemID=1 ContractParentID=8 ChargeID=687

我知道我在这里遗漏了一些东西。

public class Proposals : DbContext
    {    
        public DbSet<Charge> Charges { get; set; }            
        public DbSet<ContractParent> ContractParents { get; set; }
        public DbSet<ContractParentItem> ContractParentItems { get; set; }      
    }
4

1 回答 1

1

代替

public virtual IEnumerable<ContractParentItem> ContractParentItems { get; set; }

你应该有

public virtual ICollection<ContractParentItem> ContractParentItems { get; set; }
于 2013-10-17T20:50:11.473 回答