-1

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.

Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152

1 Answers1

1

You should not need to specify the ForeignKey due to the standardized naming you are using. If you remove the ForeignKey attributes, your application shoudl not run in to this issue.

This link should show you how EF core's defaults work and how to specify the foreign key if you use more complex naming. https://www.entityframeworktutorial.net/code-first/foreignkey-dataannotations-attribute-in-code-first.aspx

TeaBaerd
  • 1,104
  • 11
  • 16