0

I'am trying to map on code first this tables:

Table Agent(
    int AgentId PK,
    --...
)

Table Group(
    int GroupId PK,
    varchar Name,
)

Table GroupAgent(
    int AgentId PK,
    int GroupId
)

But now, I don't know How Can I Map this:

public class Agent{
   public int Id {get; set;}
   public string Name {get; set;}
   public virtual Group AgentGroup {get; set;}
}

public class Group{
    public int Id {get; set;}
    public List<Agent> Agents {get; set;}
}

Someone Can Help me?

Anders Abel
  • 66,163
  • 17
  • 148
  • 213
landrady
  • 149
  • 1
  • 6
  • Your tables show a many-to-many but your entities show a single-to-many. Which do you want? If you want the single-to-many, you already have it. – Neil Smith Jul 03 '14 at 20:13
  • Having a `GroupAgent` table allows for a many to many relationship and EF will want to map it as such. Additionally, you should be using `ICollection` not `List`. – cadrell0 Jul 03 '14 at 20:13
  • @Smith.h.Neil, I would say that they show one to one. – Hamlet Hakobyan Jul 03 '14 at 20:14
  • @HamletHakobyan How? `Agent` has a `Group`. And `Group` has many `Agents`. So single-to-many. The tables show `GroupAgent` which would be a many-to-many. – Neil Smith Jul 03 '14 at 20:16
  • @Smith.h.Neil table `GroupAgent` has `AgentId` as `PK`. – Hamlet Hakobyan Jul 03 '14 at 20:17
  • @HamletHakobyan - Oh yeah, you're right about the tables. The entities are still single-to-many and I believe the question is how to create single-to-many tables from code first. – Neil Smith Jul 03 '14 at 20:19
  • Yesss, I want single-to-many, take a look that the GroupAgent table has AgentId as PK, but, I can't change my agent table to put a new field, because this I have this auxiliary table. – landrady Jul 04 '14 at 11:08

0 Answers0