簡體   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