添加拥有对象的集合

Adding collection of owned objects

我有一个包含所有类型集合的领域模型。当我尝试在 ownedtyped 集合中添加多个对象时?我得到一个例外:

System.InvalidOperationException: 'The instance of entity type 'ChildItem' cannot be tracked because another instance with the key value '{NameId: -2147482647, Id: 0}' is already being tracked. When replacing owned entities modify the properties without changing the instance or detach the previous owned entity entry first.'

如何解决?

已更新

我的域名类:

public class Parent
    {
        public int Id { get; set; }
        public Child Name { get; set; }
        public Child ShortName { get; set; }
    }

    public class Child
    {
        public List<ChildItem> Items { get; set; }
    }

    public class ChildItem
    {
        public string Text { get; set; }
        public string Language { get; set; }
    }

我的 DbContext:

public class ApplicationContext : DbContext
    {
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Parent>()
                .OwnsOne(c => c.Name, d =>
                {
                    d.OwnsMany(c => c.Items, a =>
                    {
                        a.HasForeignKey("NameId");
                        a.Property<int>("Id");
                        a.HasKey("NameId", "Id");
                        a.ToTable("ParentNameItems");
                    })
                    .ToTable("ParentName");
                })
                .ToTable("Parent");

                modelBuilder.Entity<Parent>()
                .OwnsOne(c => c.ShortName, d =>
                {
                    d.OwnsMany(c => c.Items, a =>
                    {
                        a.HasForeignKey("NameId");
                        a.Property<int>("Id");
                        a.HasKey("NameId", "Id");
                        a.ToTable("ParentShortNameItems");
                    })
                    .ToTable("ParentShortName");
                })
                .ToTable("Parent");
        }
    }

用法:

static void Main(string[] args)
        {
            var context = new ApplicationContext();

            var parent = new Parent()
            {
                Name = new Child()
                {
                    Items = new List<ChildItem>()
                    {
                        new ChildItem() { Text = "First value", Language = "en-en"},
                        new ChildItem() { Text = "Second value", Language = "en-en"}
                    }
                },
                ShortName = new Child()
                {
                    Items = new List<ChildItem>()
                    {
                        new ChildItem() { Text = "First short value", Language = "en-en"},
                        new ChildItem() { Text = "Second short value", Language = "en-en"}
                    }
                }
            };

            context.Set<Parent>().Add(parent);

            context.SaveChanges();
        }

好吧,首先你类没有意义。如果有的话,它应该是

public class Parent
{
    public int Id { get; set; }
    public List<Child> Children { get; set; }
}

a parent 应该有很多 children(或者 none 可能,也许是空列表?)。 child 的名字和 id 呢,他没有一个吗?也可能是 ParentId 可能类似于

public class Child
{
    public virtual List<ChildItem> Items { get; set; } //why virtual? you planning to inherit?
    public string Name {get; set; }
    public int Id {get; set; }
    public int ParentId {get; set; }
}

我认为这看起来好多了。数据库应该有匹配的表。老实说 EF (Entity Framework) auto create 将为您完成 99% 的工作,所以请使用它。

问题已解决。排队

a.HasKey("NameId", "Id");

OnModelCreating 方法中。 我使用了一个 example,其中写了关于配置拥有类型的集合。

从键定义中删除 "NameId" 字段后

a.HasKey("Id");

现在一切正常。