我尝试了同时将实体映射为one-to-one
和one-to-many
的不同方法,但我没有运气。
我在这里提供了我的模型:
public class Office
{
public virtual Person Manager {get; set;}
public virtual List<Person> People {get; set;}
}
public class Person
{
public virtual Office Office{get;set;}
}
有人可以指导我通过流畅的api写地图吗?
答案 0 :(得分:-1)
没有必要为特定实体定义两个不同的映射。我只需要通过流畅的api映射one-to-many
关系。
以下是更改:
public class Office
{
// fk field to the entity
public Guid ManageId{get;set;}
public virtual Person Manager {get; set;}
public virtual List<Person> People {get; set;}
}
public class Person
{
// fk field to the entity
public virtual Guid OfficeId{get;set;}
public virtual Office Office{get;set;}
}
并使用流利的api:
modelBuilder.Entity<Office>()
.HasMany(d => d.People)
.WithOptional(s => s.Manager)
.Map(cs => cs.MapKey("OfficeId"));