2

Given entities like these:

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

Normally, Entity.EntityTwos would return all EntityTwo entities where EntityId equals Entity.Id. Is it possible to rig up the model so that property returns entities that join on that Id, but also have EntityTwo.TypeId == 2 (essentially adding a where clause to the join)?

Ocelot20
  • 10,510
  • 11
  • 55
  • 96

2 Answers2

1

You would have to do a manual join rather than relying on the navigational properties to create an implied join.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
0

Maybe you can solve this by thinking about it a different way. Your join keys would be:

{ TypeId, Id } equals { 2, Id }

Maybe it's possible to use a constant as one of the two composite keys? See Ladislav's composite key as foreign key answer.

Community
  • 1
  • 1
Scott Stafford
  • 43,764
  • 28
  • 129
  • 177
  • Seemed promising, but the composite mapping requires two *properties* (adding a property on the entity with a constant value didn't work either). This similar question/answer seems to confirm: http://stackoverflow.com/questions/11526497/literal-or-constant-as-part-of-composite-key-in-ef-code-first – Ocelot20 Sep 07 '12 at 12:12