How to create a 0..1 to 0..1 relation between two entities

Posts   
 
    
chrissie
User
Posts: 84
Joined: 05-Oct-2010
# Posted on: 13-Sep-2011 14:39:36   

Hi all,

ef 4, vs 2010 sp1, sql server 2008 r2, w7 sp1, .net fw 4

I defined a 0..1 to 0..1 relation between two entities. if I now change to the code and try to create a new object and save it to the db, I get a foreign key violation exception. Why? And what do I have to do to avoid this?

Thanx in advance, best regards

Chris

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 14-Sep-2011 01:21:52   

Hi Chris,

Please describe the scenario. i.e.: - Is that a model-only relation (you created it in LLBLGen Designer) or LLBLGen fetched the relation from your relational database? - What are those two entities? Which are the PK and the FK. - Post the code snippet that raises the FK violation exception. - Post the complete exception message and stack trace. - LLBLGen version and runtime library version? - In general: http://llblgen.com/tinyforum/Messages.aspx?ThreadID=7725

David Elizondo | LLBLGen Support Team
chrissie
User
Posts: 84
Joined: 05-Oct-2010
# Posted on: 14-Sep-2011 12:02:07   

Hi David,

to be clear, here the question again:

What do I have to do to generate a foreign key constraint with NOcheck option for the 0..1 ends of a 0..1 to 0..1 relation between two entities in LLBLGen Pro 3.1 (newest release) into the DDL script for sql server 2008 r2?

best regards

Chris

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 14-Sep-2011 16:19:13   

I think you mean nullable Fk, right? If so then please check the Optional flag, of that field.

chrissie
User
Posts: 84
Joined: 05-Oct-2010
# Posted on: 14-Sep-2011 16:45:33   

Hi Walaa,

this is what i thought, too. But this does not work and when i take a look at the templates used to generate the ddl script file, i can not find the template which generates the foreign key constraint with nocheck option. If there is a template in place which is responsible for generating a foreign key constraint with nocheck option, please let me know which one it is.

Thank you and best regards

Chris

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 14-Sep-2011 19:06:31   

Which Designer version are you using?

chrissie
User
Posts: 84
Joined: 05-Oct-2010
# Posted on: 15-Sep-2011 09:32:05   

Hi Walaa,

this was not the answer I hoped for. How many designer versions are in place? What is a designer version?

For sure, I will answer your question when I understand them. In the meanwhile could please so kind and just answer my question for the newest designer version?

Thank you very much for your effort, best regards

Chris

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 15-Sep-2011 10:47:09   

I can't reproduce it with the newest Designer version. That's why I was asking for the version you are using.

When I generate the DDL Create SQL, the FK field gets the "NULL" keyword generated instead of the "Not NULL". Since it was marked optional.

Designer version = release date in the Main window Title bar, also you can find it in Help/About.

Mine is (June 17th 2011).

chrissie
User
Posts: 84
Joined: 05-Oct-2010
# Posted on: 19-Sep-2011 18:23:06   

Hi Walaa,

perhaps it is possible to tell me which the name of the template is, that creates the NOcheck option for foreign key constraints. I would be glad and happy,

thank you very much in advance, best regards

Chris

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 19-Sep-2011 18:34:26   

I don't think you need "NoCheck".

The "NoCheck" disables the Constraints, it's only useful to use temporarily when migratng data, but not on production.

You need the FK constraint to check for invalid values. (values inserted that doesn't exist in the PK table).

What you need is (0,1 - m) relationship, which means either the FK has a valid value, or it does not have a value.

For that all you need is to allow null for the FK. If null is passed, the FK contraint will not be checked. If another a value (not null) is passed, the FK Constraint is checked.

Having said that: Please make sure you get "NULL" generated for the FK when you check the Optional checkbox.