简体   繁体   中英

Entity Framework Core: Owned type that has navigation properties

I have some classes:

public class Project 
{
     public TimeLine TimeLine { get; set; };
}

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

public TimeLinePhase 
{
}

The class Project owns TimeLine and this all works fine until I specify the navigation between TimeLine and TimeLinePhase .

Fluent API code that specifies TimeLine is owned:

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

I get this error:

InvalidOperationException: Unable to determine the relationship represented by navigation 'TimeLine.TimeLinePhase' of type 'ICollection'. Either manually configure the relationship, or ignore this property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'.

So then I specify the relation between TimeLine and TimeLinePhase like this:

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

But now I get this error:

The entity type 'TimeLine' cannot be configured as non-owned because it has already been configured as a owned. Use the nested builder in OwnsOne or OwnsMany on the owner entity type builder to further configure this type

How can I have TimeLine as an owned type, and still have the relation between TimeLine and TimeLinePhases ?

I brief search revealed an answer. It seems configuring an owned type having a collection is not allowed. The problem being an owned type cannot act as a principal entity for the collection

See comments by AjVickers at this GitHub page: https://github.com/do.net/efcore/issues/27175

It seems a redesigned datastructure is needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM