Print Page | Close Window

Adding Entity via Many To Many No Payload

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=3091
Printed Date: 13-May-2026 at 9:42pm


Topic: Adding Entity via Many To Many No Payload
Posted By: gregweb
Subject: Adding Entity via Many To Many No Payload
Date Posted: 13-Nov-2011 at 9:39am
I have a many to many with no payload set up. The Navigation properties show up just fine in the EDMX, and all displays correctly in the gui if I manually create the records and association record in the db.

The data setup is   Contacts --> ContactCompany <-- Company

All of this works fine until I try to add a company in code. The company entity is created and saved, but no record is created in the ContactCompany table, so the association is not created.

I downloaded and tried the ManyToMany No Payload sample. It has a function in it where it creates a new Territory and it associates it with an employee. I modified it to SaveChanges() to see if that works, but it does not seem to actually create the many to many record. It does create the Territory Entity, but the association is lost. So it appears there is a bug in this sample.

This is my code:


CompanyEntity newEntity = new CompanyEntity(this.SelectedContact.OwnedBy);
this.ContactEntityManager.AddEntity(newEntity);
this.SelectedContact.CompanyEntities.Add(newEntity);
this.ContactEntityManager.SaveChangesAsync();

Note this is NOT using codefirst, just the regular edmx.

Greg



Replies:
Posted By: gregweb
Date Posted: 13-Nov-2011 at 11:15am
Note that I did get this working by using a Payload and intermediate entity and creating it manually. This works for me, so it's not an issue at this time.

Greg


Posted By: DenisK
Date Posted: 16-Nov-2011 at 9:40am
Hi Greg;

I just tested the sample and was able to see the many-to-many records added and deleted appropriately on the mapping table. The mapping table is EmployeeTerritoryNoPayload, and not EmployeeTerritory. Could you confirm that you were looking at the right mapping table?


Posted By: gregweb
Date Posted: 17-Nov-2011 at 12:51pm
Hi Denis,

I tried this again, and you are correct - it is working - I was looking at the right table, but the record was inserted at the top of the table instead of the bottom where I was looking for it. Not sure why, but no matter, it is indeed working properly.

Greg



Print Page | Close Window