(These tests and errors were written against EF 6.1.3)
First Attempt
[Table("Review")]
public class Review
{
[Key]
public int Id { get; set; }
public PictureInfo PictureInfo { get; set; }
}
[Table("Review")]
public class PictureInfo
{
[Key]
public int Id { get; set; }
public Review Review { get; set; }
}
With the above entities I received this error:
Unable to determine the principal end of an association between the
types. The principal end of this association must be explicitly
configured using either the relationship fluent API or data
annotations.
Second Attempt
[Table("Review")]
public class Review
{
[Key]
public int Id { get; set; }
[Required]
public PictureInfo PictureInfo { get; set; }
}
[Table("Review")]
public class PictureInfo
{
[Key]
public int Id { get; set; }
[Required]
public Review Review { get; set; }
}
The entity types 'Review' and 'PictureInfo' cannot share table
'Review' because they are not in the same type hierarchy or do not
have a valid one to one foreign key relationship with matching primary
keys between them.
Third Attempt
[Table("Review")]
public class Review
{
[Key]
public int Id { get; set; }
[Required, ForeignKey("Id")]
public PictureInfo PictureInfo { get; set; }
}
[Table("Review")]
public class PictureInfo
{
[Key]
public int Id { get; set; }
[Required, ForeignKey("Id")]
public Review Review { get; set; }
}
Unable to determine the principal end of an association between the
types. The principal end of this association must be explicitly
configured using either the relationship fluent API or data
annotations.
Working Code
[Table("Review")]
public class Review
{
[Key, ForeignKey("PictureInfo")]
public int Id { get; set; }
public PictureInfo PictureInfo { get; set; }
}
[Table("Review")]
public class PictureInfo
{
[Key, ForeignKey("Review")]
public int Id { get; set; }
public Review Review { get; set; }
}