views:

25

answers:

1
  • I have a product class (created from EF)
  • I have a user class (created from EF)
  • A user can have one to many products (UserProduct object created from EF with foreign keys)

What I want to do is

  1. Create two Products (save off so they have IDs)
  2. Create a User (don't save yet)
  3. Create two UserProducts to reference to User
  4. SaveChanges so that all three get updated

Before I was inserting step 1 and step 2 first, getting the ids, setting up the entitykeyreference for step 3 and saving UserProducts. Doesn't seem like it needs to be this way!

+2  A: 
var p1 = new Product();
var p2 = new Product();
var u = new User();
context.AddObject("Users", u);
u.UserProducts.Add(p1);
u.UserProducts.Add(p2);
context.SaveChanges();
Craig Stuntz