Print Page | Close Window

Add/Insert & one to one mappings (SL)

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=2365
Printed Date: 13-May-2026 at 2:59am


Topic: Add/Insert & one to one mappings (SL)
Posted By: pompomJuice
Subject: Add/Insert & one to one mappings (SL)
Date Posted: 10-Dec-2010 at 1:54am

Hi.

I am trying to achieve a one to one mapping in my data. Firstly I set up this relationship in the database by setting one table's primary key as a foreign key into another table.
 
Entity1.PK  <-------- Entity2.PK/FK
 
Ideablade imports the model and everything seems to work until you try to do an insert Entity1/Entity2 pair in one transaction. An exception is thrown: A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column:Entity1.PK"
 
A Entity1/Entity2 pair insert within one transaction works fine if you relate them in a one to many fashion though:
 
                               Entity2.PK
Entity1.PK <--------- Entity2.FK
 
... where I presume EntityManger or EF 4.0 first insterts Entity1, extracts its generated PK and use that when inserting Entity2. I'm guessing.
 
I am assuming this does not work with one to one relationships? Do I have to manually commit entity1, extract its PK so that I can use it to insert Entity2. This makes reject changes more compicated etc etc. What do I do here?
 



Replies:
Posted By: pompomJuice
Date Posted: 10-Dec-2010 at 5:53am
Aah, never mind.
 
I found the problem.
 
If you create the FK relationship Entity1.PK <------- Entity2.PK/FK then you must drag the mouse from entity1.PK to entity2.PK/FK when you are in SSMS's Database Diagram Mode. Basically my foreign key was the wrong way around and I failed to spot it.
 
Usually when you make FKs you drag the FK from the Foreign Key Base Table to the Primary/Unique Key Base table's PK. But when your FK is a PK, you have to drag it the other way around for some reason. Bizarre.


Posted By: olegasd
Date Posted: 31-Aug-2012 at 8:05am
Originally posted by pompomJuice

Aah, never mind.
 
I found the problem.
 
If you create the FK relationship Entity1.PK <------- Entity2.PK/FK then you must drag the mouse from entity1.PK to entity2.PK/FK when you are in SSMS's Database Diagram Mode. Basically my foreign key was the wrong way around and I failed to spot it.
 
Usually when you make FKs you drag the FK from the Foreign Key Base Table to the Primary/Unique Key Base table's PK. But when your FK is a PK, you have to drag it the other way around for some reason. Bizarre.


Thanks a lot! It was helpful for me, my problem solved. Thanks!




Print Page | Close Window