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

扶醉桌前 提交于 2019-12-22 08:52:50

问题


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?


回答1:


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);


来源:https://stackoverflow.com/questions/51808912/the-child-dependent-side-could-not-be-determined-for-the-one-to-one-relationship

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!