1

In my Code First for this application I have defined a foreign key as nullable. However, when I try to add a record without that key, I still get a Foreign Key constraint error:

The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_dbo.RequestReview_dbo.CustomForm_ReviewFormId\". The conflict occurred in database \"Mkp\", table \"dbo.CustomForm\", column 'CustomFormId'.\r\nThe statement has been terminated.

How should I be defining the relationship so that the key constraint is not enforced?

My code first model defines the field like this:

[Key, ForeignKey("Resource"), DatabaseGenerated(DatabaseGeneratedOption.None)]
public Guid RequestReviewId { get; set; } // My Primary Key
public virtual Resource Resource { get; set; }

public Guid? ReviewFormId { get; set; } // Foreign Key
[ForeignKey("ReviewFormId")]
public CustomForm ReviewForm { get; set; }

(I did try searching, but I'm not sure I searched with the right terminology.)

Edit/Update:
If I remove the ForeignKey tag, I still get migration trying to create a relationship, but this time called ReviewForm_CustomFormId. How can I avoid this?

Updated version of the model:

[Key, ForeignKey("Resource"), DatabaseGenerated(DatabaseGeneratedOption.None)]
public Guid RequestReviewId { get; set; } // My Primary Key
public virtual Resource Resource { get; set; }

public Guid? ReviewFormId { get; set; } // Foreign Key
public CustomForm ReviewForm { get; set; }
M Kenyon II
  • 4,136
  • 4
  • 46
  • 94

2 Answers2

0

One solution i see is that you could use this colunm ReviewFormId as an implicite foreign key. What i mean is : don't define it as a foreign key, just a regular data instead, and only YOU will know that the data inside it represent a foreign key, but don't tell entity that it is a foreign key because GUID just dosen't accept nullable foreign key.

Antoine Pelletier
  • 3,164
  • 3
  • 40
  • 62
  • I was hoping there was a better way. Also, if I drop the ForeignKey attribute, migration wants to create a new column, `ReviewForm_CustomFormId` which would have the same foreign key integrity thing. – M Kenyon II Nov 19 '15 at 21:48
  • Just to be clear, you can have nullable foreign keys. It is the GUID that is the issue. http://stackoverflow.com/questions/26000345/allow-null-foreign-key-guid – Steve Greene Nov 19 '15 at 21:58
  • @SteveGreene your rigth, of course you can have nullable foreign keys, but this dosen't really match with using Entity Framework, I've been through this problem a hundred time, and if you found any better way to make GUID work, put the link here please ! – Antoine Pelletier Nov 20 '15 at 14:21
0

Give it a separate primary key field as well as your foreign key:

public int ID { get; set; }
public Guid? ReviewFormId { get; set; }
[ForeignKey("ReviewFormId")]
public CustomForm ReviewForm { get; set; }

Doing this I was able to insert records that have a null ReviewFormId.

DrewJordan
  • 5,266
  • 1
  • 25
  • 39
  • Sorry, I had a primary key, I just didn't show it in the sample. I have added the key I am using in the sample, above. This was not the answer. – M Kenyon II Nov 20 '15 at 13:46