实体框架核心:拥有具有导航属性的类型



我有一些类:

public class Project 
{
public TimeLine TimeLine { get; set; };
}
public class TimeLine 
{
public ICollection<TimeLinePhases> TimeLinePhases { get; set; };
}
public TimeLinePhase 
{
}

Project拥有TimeLine,这一切都很好,直到我指定了TimeLineTimeLinePhase之间的导航。

指定拥有TimeLine的Fluent API代码:

builder.Owned<TimeLine>();
builder.entity<Project>().OwnsOne(p => p.TimeLine);

我得到这个错误:

InvalidOperationException:无法确定导航的TimeLine表示的关系。"ICollection"类型的"TimeLinePhase"。手动配置关系,或使用"[NotMapped]"特性或使用"EntityTypeBuilder"忽略此属性。忽略"OnModelCreating"中的"。

因此,我指定TimeLineTimeLinePhase之间的关系如下:

builder
.Entity<TimeLine>()
.HasMany(t => t.TimeLinePhase)
.WithOne()
.IsRequired(false);

但现在我得到了这个错误:

实体类型"TimeLine"无法配置为非所有者,因为它已配置为所有者。在所有者实体类型生成器上使用OwnsOneOwnsMany中的嵌套生成器来进一步配置此类型

如何将TimeLine作为所拥有的类型,并且仍然具有TimeLineTimeLinePhases之间的关系?

我简短的搜索找到了答案。似乎不允许配置具有集合的所属类型。问题是拥有的类型不能作为集合的主体

请参阅AjVickers在此GitHub页面上的评论:https://github.com/dotnet/efcore/issues/27175

似乎需要重新设计数据结构。

最新更新