Reputation: 7479
I am using database first.
I am getting the error:
An error occurred while saving entities that do not expose foreign key properties for their relationships. The EntityEntries property will return null because a single entity cannot be identified as the source of the exception. Handling of exceptions while saving can be made easier by exposing foreign key properties in your entity types.
My table has 2 of the same objects (address in this case). 1 is null, 1 is not. I tried suggestions I found on similar posts (Make FK Ids nullable in the db), but to no avail.
Table FKs are: ResidenceAddressId, MailingAddressId
With FK relationships to Table "Address"
Class is called "Address"
Main object has the properties:
public Address ResidenceAddress { get; set; }
public Address MailingAddress { get; set; }
I assume I need to do some kind of mapping, but I cannot figure out where/how.
Upvotes: 1
Views: 50
Reputation: 462
Add this to your object:
public int ResidenceAddressId { get; set; }
public int MailingAddressId { get; set; }
and then add annotations:
[ForeignKey("ResidenceAddressId")]
public Address ResidenceAddress { get; set; }
[ForeignKey("MailingAddressId")]
public Address MailingAddress { get; set; }
Upvotes: 1