One-to-One Failover Relationships with Integer Keys in LINQ-to-SQL

I have two objects ( Foo and Bar ) that have a one to zero or one relationship between them. Thus, Foo has a link to a NULL reference to Bar.ID and a unique ( nullbusted ) unique index to force the use of side "1". Bar.ID is an int, and therefore Foo.BarID is a null int.

The problem occurs when matching DBML LINQ-to-SQL .NET types with SQL data types. Since int not a null type in .NET, it terminates in a Nullable<int> . However, this is not the same type as int , so Visual Studio gives me this error message when I try to create the OneToOne Association between them:

Unable to create association "Bar_Foo". Properties do not have the appropriate types: "ID", "BarID".

Is there any way around this?

+4
source share
1 answer

So far I have been working on this by setting Bar.ID (primary key) to CanBeNull="true" , which is definitely ugly. I hope for a better solution here.

+2
source

Source: https://habr.com/ru/post/1311461/


All Articles