繁体   English   中英

子对象未保存到数据库中,先使用实体​​框架代码

[英]Child object not saving into the DB, using Entity Framework Code First

我有一个具有子对象集合的父对象。 我想使用EF Code First将这些数据保存到数据库中。 由于某些奇怪的原因,父对象被保存,而子对象未被保存。

这是我的代码:

class Person
{
        public Person()
        {
            Operations = new ICollection<Operation> ();
        }

    public int Id { get; set; }

    public virtual ICollection<Operation> Operations { get; set; }
}


public class Operation
{
    public int Id { get; set; }

    public string Data { get; set; }

    public int PersonId { get; set; }

    public virtual Person Person{ get; set; }
}

这是DAL UpdateMethod:

public bool UpdatePerson entity)
{

   //_context.Set<Person>().AddOrUpdate(entity);
   var entityInDb = _context.Persons.Find(entity.Id);

   _context.Entry(entityInDb).CurrentValues.SetValues(entity);
   _context.Entry(entityInDb).State = EntityState.Modified;

   return _context.SaveChanges() > 0;
}

这是BLL:

   //Create a new person if it does not exist. If exists, add only the Operations:

       //check if such the person exists in the DB
       var person = Repository.Filter(p=>p.Id == Id)
                    .AsNoTracking().FirstOrDefault();


        //if not, create a new record
        if(person == null)
        {
            personNew = new Person{
                 ...
            };
            bool res = Repository.Insert(personNew );

            if (res)
            {
                //find newly created person
                person = Repository.Filter(p => p.Id == ...)
                      .AsNoTracking().FirstOrDefault();
            }
        }

//Add Operations to the Person entity and Save them:    
        var op = obj.ToOperationModel();
        op.PersonId = person.Id; 
        person.Operations.Add(op);

        var res2 = Repository.Update(person);

没有错误,创建了父对象(Person),res2返回true,但是没有将操作添加到数据库中

像这样修改更新方法;

public bool UpdatePerson entity)
{

   //_context.Set<Person>().AddOrUpdate(entity);
   var entityInDb = _context.Persons.Include(x => x.Operations).FirstOrDefault(x => x.Id == entity.Id);//Add Include for navigation property

   _context.Entry(entityInDb).CurrentValues.SetValues(entity);
   _context.Entry(entityInDb).State = EntityState.Modified;
   _context.Entry(entityInDb).Property(x => x.Operations).IsModified = true; // Add this line
   return _context.SaveChanges() > 0;
}

要建立实体之间的关系,可以使用DataAnnotations属性或Fluent Api。 要使用DataAnnotations关联关系,请像这样修改OperationPerson实体;

public class Operation
{
    public int Id { get; set; }

    public string Data { get; set; }

    [ForeignKey("Person")] // Add ForeignKey attribute
    public int PersonId { get; set; }

    public virtual Person Person{ get; set; }
}
public class Person
{
    public Person()
    {
        Operations = new ICollection<Operation> ();
    }
    [Key] // Add key attribute
    public int Id { get; set; }

    public virtual ICollection<Operation> Operations { get; set; }
}

暂无
暂无

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

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