EF Code First:带有Where子句的外键?

时间:2012-09-06 21:28:28

标签: .net linq entity-framework linq-to-entities ef-code-first

鉴于以下实体:

public class Entity
{
    public int Id { get; set; }
    public ICollection<EntityTwo> EntityTwos { get; set; }
}

public class EntityTwo
{
    public int Id { get; set; }
    public int TypeId { get; set; }
    public int EntityId { get; set; }
}

通常,Entity.EntityTwos会返回EntityId等于Entity.Id的所有EntityTwo实体。是否可以装配模型,以便属性返回加入该Id的实体,但也有EntityTwo.TypeId == 2(基本上为连接添加where子句)?

2 个答案:

答案 0 :(得分:1)

您必须进行手动连接,而不是依赖导航属性来创建隐含连接。

答案 1 :(得分:0)

也许你可以通过不同的方式来解决这个问题。您的加入密钥将是:

{ TypeId, Id } equals { 2, Id }

也许可以将常量用作两个复合键之一?请参阅Ladislav的composite key as foreign key答案。