Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
359 views
in Technique[技术] by (71.8m points)

Self referencing table with Entity Framework Core 5

I have a self-referencing entity in my app. A package type with dependencies (as a list) to other package types. I am trying to design the required intermediate table with the model builder. Unfortunately I can't manage to fill the key correctly.

System.InvalidOperationException: 'Unable to track an entity of type 'PackageDependency (Dictionary<string, object>)' because its primary key property 'DependencyId' is null.'

    public class PackageType
    {
        [Key]
        public String Tag { get; set; }

        public virtual List<PackageType> Dependencies { get; set; } = new List<PackageType>();
    }

DB Context:

 protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<PackageType>()
                .HasMany(p => p.Dependencies)
                .WithMany(p => p.Dependencies)
                .UsingEntity<Dictionary<string, object>>(
                    "PackageDependency",
                    j => j
                        .HasOne<PackageType>()
                        .WithMany()
                        .HasForeignKey("PackageId")
                        .HasConstraintName("FK_PackageDependency_PackageId")
                        .OnDelete(DeleteBehavior.Cascade),
                    j => j
                        .HasOne<PackageType>()
                        .WithMany()
                        .HasForeignKey("DependencyId")
                        .HasConstraintName("FK_PackageDependency_DependencyId")
                        .OnDelete(DeleteBehavior.ClientCascade),
                    e => e.HasKey("PackageId", "DependencyId"));
        }

Does anyone have a hint for me?

Thanks a lot!

question from:https://stackoverflow.com/questions/65944269/self-referencing-table-with-entity-framework-core-5

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm surprised EF Core even allowed this

.HasMany(p => p.Dependencies)
.WithMany(p => p.Dependencies)

(using one and the same collection in for both sides of the relationship) without reporting an error.

Many-to-many require 2 collection navigation properties - one for each side of the relationship. So even though this is self referencing relationship, it still requires 2 separate collection navigation properties bound to the corresponding join entity FKs. e.g.

public virtual List<PackageType> ChildDependencies { get; set; } = new List<PackageType>();
public virtual List<PackageType> ParentDependencies { get; set; } = new List<PackageType>();

and

.HasMany(p => p.ChildDependencies) // -> j.PackageId
.WithMany(p => p.ParentDependencies) // -> j.DependencyId

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...