Reputation: 232
I am desperately trying to implement a nullable foreign key with the Entity Framework model designer.
The tables in question look like this:
The settings should be correct, as my class is generated accordingly with the Sender
property as follows:
public Nullable<int> SenderId { get; set; }
However, when calling Context.Database.Create();
the created databases SenderId
property is not nullable.
If I manually change the columns type to a nullable in the database, I get an error within EF that the model and the database differ.
Any advice?
Thanks!
Upvotes: 2
Views: 2035
Reputation: 232
Okay, after messing around for a few hours, I finally got to the source of the problem: the edmx-file of my Entity model.
While the Visual Studio Entity Model Designer showed me that the column is nullable (or rather I set it to nullable), I had to find out that the .edmx file itself didn't apply these changes. I manually changed the .edmx file within an editor.
<Property Name="SenderId" Type="Int32" Nullable="true" />
The Nullable property was originally set to false... By the way: the associations weren't saved either, I had to set the 0..1 association myself within the text editor, since the foreign key is now nullable. Thanks for the help anyway!
Upvotes: 0
Reputation: 301
I had the same problem like you
Try to set your senderid like this:
public int? SenderId { get; set; }
If this doesnt help you, you can change the property as shown above and modify the database manually, after you've done that. there should be no difference.
Upvotes: 2