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!

1 Answer 1

4
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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.