views:

197

answers:

1

I have three tables in my database: An A table, a B table, and a many-to-many ABMapping table. For simplicity, A and B are keyed with identity columns; ABMapping has just two columns: AId and BId.

I built an Entity Framework 4 model from this, and it did correctly identify the N:M mapping between A and B. I then built a WCF Data Service based on this EF model.

I'm trying to consume this WCF Data Service. Unfortunately, I can't figure out how to get a mapping between As and Bs to map back to the database. I've tried something like this:

A a = new A();
B b = new B();
a.Bs.Add(b);
connection.SaveChanges();

But this doesn't seem to have worked. Any clues? What am I missing?

+1  A: 

You need to do the following:

A a = new A();
B b = new B();

connection.AddObject("ASet", a);
// if you have the generated code, you can use the helper method generated
// on the context - something like connection.AddToASet(a);

connection.AddRelatedObject(a, "Bs", b);
connection.SaveChanges();
Pratik Patel