Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The child/dependent side could not be determined for the one-to-one relationship

Tags:

I am trying to update my database with "update-database" command in package manager console, But I have this kind of error:

The child/dependent side could not be determined for the one-to-one  relationship between 'Country.CapitalCity' and 'CapitalCity.Country'. 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. 

My Model classes look like this:

public class Country {     public int ID { get; set; }     public string Name { get; set; }     public long Population { get; set; }      public int CapitalCityID { get; set; }     public CapitalCity CapitalCity { get; set; } }  public class CapitalCity {     public int ID { get; set; }     public int Name { get; set; }      public int CountryID { get; set; }     public Country Country { get; set; } } 

After searching info about this problem, I added the following code in my DbContextModelSnapshot, but I still have problem.

modelBuilder.Entity<Country>()             .HasOne(a => a.CapitalCity)             .WithOne(a => a.Country)             .HasForeignKey<CapitalCity>(c => c.CountryID); 

What mistake do I have?

like image 513
A.M Avatar asked Aug 12 '18 12:08

A.M


People also ask

How to make one to one relationship in EF Core?

Entity Framework Core will create a one to one relationship when both entities involved in the relationship contain a navigation property to the other, and the dependent entity includes a foreign key property for the principal entity.

What is OnModelCreating in Entity Framework?

The DbContext class has a method called OnModelCreating that takes an instance of ModelBuilder as a parameter. This method is called by the framework when your context is first created to build the model and its mappings in memory.


2 Answers

You have to put the below code in your DBContext class, not in SnapShot class. Don’t modify the Snapshot class, it’s auto generated class.

modelBuilder.Entity<Country>()             .HasOne(a => a.CapitalCity)             .WithOne(a => a.Country)             .HasForeignKey<CapitalCity>(c => c.CountryID); 
like image 73
vivek nuna Avatar answered Sep 20 '22 20:09

vivek nuna


Just to point it out if you don't really need a one-to-one relationship you can solve it like this and the migration will work out of the box. Valid if a CapitalCity can share the same name with different Countries in this example. You would of course need to factor the possibility that one country changes its capital city name but the other country does not.

public class Country {     public int ID { get; set; }     public string Name { get; set; }     public long Population { get; set; }      public int CapitalCityID { get; set; }     public CapitalCity CapitalCity { get; set; } }  public class CapitalCity {     public int ID { get; set; }      public int Name { get; set; } } 

In this case the better solution would probably be like this though:

public class Country {     public int ID { get; set; }     public string Name { get; set; }     public long Population { get; set; }      public List<CapitalCity> CapitalCities { get; set; } = new List<CapitalCity>(); }  public class CapitalCity {     public int ID { get; set; }     public int Name { get; set; }      public int CountryID { get; set; }     public Country Country { get; set; } } 

I think it is better because a lot of countries have or had multiple capital cities.

https://en.wikipedia.org/wiki/List_of_countries_with_multiple_capitals

like image 24
Ogglas Avatar answered Sep 23 '22 20:09

Ogglas