LINQ to SQL – Nullable INT in ForeignKey = “Cannot create an association…”

associationsforeign-keyslinq-to-sql

I have a table that has a primary key that's an INT… I have another table, that has a foreignkey relationship to that first table, but it's a NULLABLE INT.

This is perfectly ok, and 100% acceptable to SQL… however LINQ to SQL is complaining about mismatched types ("int to Nullable[int]").

Error Message: Cannot create an association "Store_People". Properties do not have matching types: "PersonID", "ID".

How do I solve this so that the designer stops yelling at a 100% correct usage of SQL?

Best Answer

I found the problem. You can create the association ONLY if the parent class doesn't have the Nullable field as the key.

So, this:

alt text http://www.singingeels.com/Articles/UserImage.aspx?ImageID=43d83ac4-707d-47fc-832c-588261e6a5d7

will cause this error:

alt text http://www.singingeels.com/Articles/UserImage.aspx?ImageID=19ed54aa-9f4f-4d5c-ba18-4996509d2da7

...but this:

alt text http://www.singingeels.com/Articles/UserImage.aspx?ImageID=0e09affd-2134-4712-999b-7e792f6e081b

will work perfectly.

That looks like a bug (or at least, a stupidity of LINQ to SQL)... but I love it other than that.