我正在尝试使用实体框架和 ICollection 子属性更新数据库数据。
对于 INSERT 情况,EF 会自动保存子数据,但对于 Updating 情况则不会。
所以我进行了手动更新,但我想有一种我不知道的自动更新方法。
请检查我的代码,并给我建议
public class Parent
{
[Key]
public int ID {get; set;}
public string Name {get; set;}
public virtual ICollection<Child> Children{ get; set; }
}
public class Child
{
[Key]
public int ID {get; set;}
public int ParentID { get; set; }
public string Name {get; set;}
}
// INSERT 的控制器方法
public void InsertTest(){
//generate new Parent Data with child
Parent parent = new Parent() {
Name = "Nancy"
};
parent.Children.Add(new Child()
{
Name = "First Son"
});
parent.Children.Add(new Child()
{
Name = "Second Son"
});
var parentRepository = unitofwork.parentRepository;
parentRepository.insert(parent); //context.Set<Parent>().Add(parent);
unitofwork.Save();
// it save child entity well
}
// UPDATE的控制器方法
public void UpateTest()
{
//generate new Parent Data with child
Parent parent = new Parent()
{
ID = 1,
Name = "Nancy"
};
parent.Children.Add(new Child()
{
ID = 1,
ParentID = 1,
Name = "First Son Renamed"
});
parent.Children.Add(new Child()
{
ID = 2,
ParentID = 1,
Name = "Second Son"
});
// add new data
parent.Children.Add(new Child()
{
Name = "Third Son"
});
var parentRepository = unitofwork.parentRepository;
parentRepository.update(parent); //context.Set<Parent>().Attach(entityToUpdate); context.Entry(entityToUpdate).State = EntityState.Modified;
unitofwork.Save();
// it save parent data, but it does not change any for child data
// *** To make work, I did like this, ***
// var childRepository = unitofwork.childRepository;
//foreach (Child c in parent.Children.ToList())
//{
// if (c.ID < 1)
// {
// childRepository.update(c);
// }
// else
// {
// childRepository.insert(c);
// }
//}
//unitofwork.Save();
// then it works.
}