我之间有以下关系,Person
并按Country
约定定义。这完美地工作。
public class Person
{
public long PersonId {get; set;}
public string Name {get; set;}
public long CountryId {get; set;}
public virtual Country Country {get; set;}
}
public class Country
{
public long CountryId {get; set;}
public string Name {get; set;}
}
我现在希望将属性名称更改为其他名称Country
。据我了解,这意味着按照惯例,这种关系不再有效。所以我希望使用流利的语法来定义它。
我找到了这个参考,但我不清楚我应该如何处理这个确切的场景,因为在他们的例子中,关系的两边都包含一个外键或集合。
有一节“配置与一个导航属性的关系”让我认为它应该如下所示:
modelBuilder.Entity<Patient>()
.HasRequired(t => t.Country)
.WithRequiredPrincipal();
但是,每当存储国家/地区时,都会导致以下异常
{"The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_dbo.Countries_dbo.Patients_CountryId\". The conflict occurred in database \"TESTS_ffbe7a6e73a747ce8cda71f963bb20b7\", table \"dbo.Patients\", column 'PatientId'.\r\nThe statement has been terminated."}
所以我现在的问题是: - 如何使用流利的语法定义上述“按惯例”关系?- 使用流利语法时是否仍需要 CountryId 属性?