I'm trying to do a two way optional relationship between two entities:
- Series
- Fixture
A Series has an optional Fixture and a Fixture has an optional Series. Here are the two Entity classes I have so far.
public class Fixture : IEntityBase
{
public int Id { get; set; }
public DateTime Date { get; set; }
public int? SeriesId { get; set; }
public virtual Series Series { get; set; }
}
public class Series : IEntityBase
{
public int Id { get; set; }
public int? FixtureId { get; set; }
public virtual Fixture Fixture { get; set; }
}
When I try to update the database with this I get the following error:
"Unable to determine the principal end of an association between the types 'Fixture' and 'Series'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations."
So I added this line to my ModelBuilder
and the update database runs fine and the tables are created.
modelBuilder.Entity<Fixture>().HasOptional(f => f.Series).WithOptionalPrincipal(f => f.Fixture);
But when I view my database in SQL there is an unnecessary column named Fixture_Id
which makes me assume my entities setup is incorrect.
Also when I return a list of Series in debug mode I can see that one of the Series has a FixtureId as shown in the image above, but the relationship to the Fixture is null.
Hope I provided enough information.
Thanks for any help in advance.