I am trying to define a one to one relationship ,where one side has a nullable foreign key, using EF Core and i keep getting the following error:
'The child/dependent side could not be determined for the one-to-one relationship between 'Product.Transaction' and 'Transaction.Product'. To identify the child/dependent side of the relationship, configure the foreign key property. If these navigations should not be part of the same relationship configure them without specifying the inverse. See http://go.microsoft.com/fwlink/?LinkId=724062 for more details.'
I do not understand what the problem is since i just did it like i would do for a one to many relationship (placing both the Object
and its id
in both sides):
public class Product
{
[Key]
public long Id{get;set;}
[ForeignKey("transactionId")]
public Guid? TransactionId{get;set;}
public Transaction? Transaction{get;set;}
}
public class Transaction
{
[Key]
public Guid Id{get;set;}
[ForeignKey("productId")]
public long ProductId{get;set;}
public Product Product{get;set;}
}
P.S I have also checked this article and the only difference is that i am also placing the Id
as a field in both sides of the relationship.
I do that because i want easy access to the foreign key.