How to handle Foreign Keys with Entity Framework

Posted by Jack Marchetti on Stack Overflow See other posts from Stack Overflow or by Jack Marchetti
Published on 2010-05-20T18:52:58Z Indexed on 2010/05/20 19:20 UTC
Read the original article Hit count: 155

I have two entities. Groups. Pools. A Group can create many pools.

So I setup my Pool table to have a GroupID foreign key.

My code:

using (entity _db = new entity()) {
    Pool p     = new Pool();
    p.Name     = "test";
    p.Group.ID = "5";
    _db.AddToPool(p);
}

This doesn't work. I get a null reference exception on p.Group.

How do I go about creating a new "Pool" and associating a GroupID?

© Stack Overflow or respective owner

Related posts about entity-framework

Related posts about linq-to-sql