【发布时间】:2015-06-24 19:49:14
【问题描述】:
我正在尝试使用 Fluent API 进行“一对一”关联。这是我的课程:
public class Person
{
public Guid Id { get; set; }
public Guid ProfilId { get; set; }
public DateTime InsertDate { get; set; }
public DateTime UpdateDate { get; set; }
public virtual Profil Profil { get; set; }
}
public class Profil
{
public Guid Id { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public DateTime BirthDate { get; set; }
public String Email { get; set; }
public virtual Person Person { get; set; }
}
public class PersonMap : EntityTypeConfiguration<Person>
{
public PersonMap()
{
...
ToTable("Person");
HasRequired(t => t.Profil)
.WithOptional(c => c.Person)
.Map(m => m.MapKey("ProfilId"));
}
}
这个实现抛出异常Invalid column name 'ProfilId'.
有人能告诉我如何使用这些类建立具有 1-1 关系的映射吗?
谢谢
【问题讨论】:
-
你为什么要使用 1:1 的关系 - 这实际上是将逻辑上的一张表一分为二。我很感激在某些情况下您会想要这样做 - 例如,如果有很多列 - 但在我看来,这应该保留为一个表。
标签: c# entity-framework ef-fluent-api