Nullable One To One Relationships with Integer Keys in LINQ-to-SQL

Posted by Craig Walker on Stack Overflow See other posts from Stack Overflow or by Craig Walker
Published on 2010-05-31T22:47:42Z Indexed on 2010/05/31 22:53 UTC
Read the original article Hit count: 802

Filed under:
|
|
|
|

I have two objects (Foo and Bar) that have a one-to-zero-or-one relationship between them. So, Foo has a nullable foreign key reference to Bar.ID and a (nullbusted) unique index to enforce the "1" side. Bar.ID is an int, and so Foo.BarID is a nullable int.

The problem occurs in the LINQ-to-SQL DBML mapping of .NET types to SQL datatypes. Since int is not a nullable type in .NET, it gets wrapped in a Nullable<int>. However, this is not the same type as int, and so Visual Studio gives me this error message when I try to create the OneToOne Association between them:

Cannot create an association "Bar_Foo". Properties do not have matching types: "ID", "BarID".

Is there a way around this?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about LINQ