例如
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public Profile Profile { get; set; }
// public int ProfileId { get; set; }
}
public class Profile
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string PostalCode { get; set; }
}
// ...
modelBuilder.Entity<User>()
.HasOptional(x => x.Profile)
.WithRequired();
ProfileId没用,FK 在“围栏的另一边”(在 中Profile)。
(这是最有意义的国际海事组织)
如果您确实需要一个Id输入User(例如,在添加时能够Profile仅通过其 ID填写User- 如果没有真正使用一对一 - 在您创建配置文件和用户时),那么您可以反转...
modelBuilder.Entity<User>()
.HasRequired(x => x.Profile)
.WithOptional();
...而您ProfileId实际上在Id(pk -> pk) 中。