1
我在下面創建了一些基本示例代碼。我正在使用實體框架。我有一個擁有Children集合的Parent對象。我最初設立了三個孩子。當我更新孩子時,我刪除一個(約翰),並添加一個(莎拉)另外兩個(瑪麗&安妮)保持不變。考慮到後面的數據庫,有沒有辦法輕鬆做到這一點?實體框架4更新子集合
public class test
{
public test()
{
Parent father = new Parent();
father.Children.Add(new Child() { FirstName = "Mary" });
father.Children.Add(new Child() { FirstName = "Anne" });
father.Children.Add(new Child() { FirstName = "John" });
SaveEntityFramework();
}
public void UpdateChildren
{
Parent father = EntityFramework.GetParentByID(1);
// Remove John
father.Children.Remove(new Child() { FirstName = "John" });
// Add Sarah
father.Children.Add(new Child() { FirstName = "Sarah" });
// Update or do Nothing Mary & Anne
SaveEntityFramework();
}
}
public class Parent
{
public string Name { get; set; }
public List<Child> Children;
}
public class Child
{
public string FirstName { get; set; }
}
您發佈的代碼有什麼問題?它不工作? –