views:

849

answers:

2

I am a newbie. I have been able to Add new entities where there is a One-To-Many Relation. I am having a problem (don't Know how to do it) adding a new Entity when the relation is using Many-To-Many.

In my EDM I have:

  1. Orgs
    <Scalar Properties>
    a. Org_ID (Identity Field)
    b. OrgName
    c. OrgDesc
    <Navigation Properties>
    Building_orgs_Relation

  2. Buildings
    <Scalar Properties>
    a) Building_ID (Identity Field)
    b) Building_Desc
    <Navigation Properties>
    Building_orgs_Relation

  3. Org_Building_Relation
    a) Building_org_ID (Identity Field)
    b) Org_ID
    c) Building_ID
    <Navigation Properties>
    Building
    Org

I want to:

  • Insert New Orgs
  • Delete Existing Org
  • Reassign Org To different Building
  • Update Org

Can some please provide a sample on how to do it using the mentioned EDM? VB code will be appreciated.

+1  A: 

Right now, the Entity Framework is really limited in terms of what kind of many to many relationships it can handle. The only thing that the Visual Studio designer will recognize is a table consisting of only two columns, both foreign keys to the other two tables, and where the primary key is a compound key on both of the foreign keys.

So, if you have control of your database schema, one thing you could do is change your Org_Building_Relation table to drop the Building_org_ID column and make the primary key a compound key on Org_ID and Building_ID. If you do that, then when you map the tables the Entity Framework will recognize this as a many to many relationship.

If you can't do that (e.g., you don't have control of the database schema), then you will need to make sure that the only fields that you map in your EDMX are the fields which relate to the other tables, and that you do not map the primary key. This is difficult, because the mapping wizard will discard and re-create the storage mapping every time you update.

Another option would be to not use a "proper" many to many mapping in the Entity Framework and instead just treat the relationship as another entity instead of having it subsumed into the relationship.

I can't remember if this has been improved in the forthcoming .NET 4.0.

Craig Stuntz
I have a similar problem (using 3.5.1) in that I have exactly is what you are describing, Craig. But I added my mapping table after I had already added the target table. It doesn't seem to pick up the many to many map table. I add it via the wizard and it puts the map table in as annother entity.So does anyone have a link or info on how to do this proper with the 3.5.1 version?
DrydenMaker
The map table *will* be a separate entity, with relationships to the mapped entities, in 3.5 SP1.
Craig Stuntz
Figured out my problem. It was actually a schema issue. I am not an expert, but I can report on what I see. It does create an entity, but it only shows as a navigation property in the designer. So if the name of the map table is foo2barMap, you cant do a Dim baz as foo2barMap. But on a foo instance you have a foo.bar and a foo.barRefrence. You then also have a foo.foo2BarMap.Add(someBar).
DrydenMaker
One comment: the EF designer can correctly identify a mapping table with multiple columns -- but it won't correctly identify a mapping table with any columns that don't map to primary keys on the tables that it's mapping. (So you can have a multi-column primary key on your mapped tables.)
Ken Smith
A: 

Hi

I am having the same problem. I don't have Building_ID (Identity Field) field. Now I just wan't to insert and delete into Org_Building_Relation and not into the main entities. Please give me example how to perform it.

Thanks s.