简体   繁体   English

添加自有对象的集合

[英]Adding collection of owned objects

I've got a domain model with collection of owned types. 我有一个拥有自有类型集合的域模型。 When I try to add more than one object in the ownedtyped collection? 当我尝试在owntyped集合中添加多个对象时? I get an exception: 我得到一个例外:

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. System.InvalidOperationException :'无法跟踪实体类型'ChildItem'的实例,因为已经跟踪了另一个键值为'{NameId:-2147482647,Id:0}'的实例。 When replacing owned entities modify the properties without changing the instance or detach the previous owned entity entry first.' 替换拥有的实体时,在不更改实例的情况下修改属性,或首先分离先前拥有的实体条目。

How can it be solved? 怎么解决?

UPDATED 更新

My domain classes: 我的域名类:

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; }
    }

My DbContext: 我的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");
        }
    }

Usage: 用法:

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();
        }

Well, first of all you classes don't make sense. 好吧,首先你的课程没有意义。 If anything it should be 如果它应该是什么

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

a parent should have many children (or none possibly, empty list maybe?). 父母应该有很多孩子(或者可能没有孩子,可能是空名单?)。 What about the child's name and id, doesn't he have one of each ? 孩子的名字和身份证怎么样,他不是每个人都有一个? also maybe a ParentId maybe something like 也许是一个像父母一样的东西

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; }
}

That looks a little better, I should think. 我觉得这看起来好一点。 The database should have matching tables. 数据库应该有匹配的表。 and let's be honest EF (Entity Framework) auto create will do 99% of the work for you, so please use it. 而且说实话EF(实体框架) 自动创建将为您完成99%的工作,所以请使用它。

Problem is solved. 问题解决了。 It was in line 它符合要求

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

in OnModelCreating method. OnModelCreating方法中。 I used an example where was written abount configuring Collections of owned types. 我使用了一个示例 ,其中写了abount配置自有类型的集合。

After deleting "NameId" field from Key definition 从密钥定义中删除“NameId”字段后

a.HasKey("Id");

everything works fine now. 现在一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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