使用EF 4.1将父记录和多个子记录插入单个“工作单位” [英] Using EF 4.1 Inserting a parent record and multiple child records as a single "Unit of Work"
问题描述
public class Parent
{
public int Id(get; set;}
public string name {get; set;}
public List< Child> Children {get; set;}
public Parent()
{
Children = new List& Child>();
}
}
public class Child()
{
public int Id(get; set;}
public string Name {get; set;}
}
public class MyContext:DbContext
{
public DbSet< Parent> Parents {get; set;}
public DbSet< Child>儿童{get; set;}
}
简单的控制台应用程序我创建一个父项并创建两个子记录,并将它们添加到父项,如果然后调用SaveChanges(),则只有第一个孩子被添加到children表中。
var x = new MyContext();
var c1 = new Child {Name =Alpha-Child};
var c2 = new Child {Name =Beta-Child};
var p = new家长{Name =Alpha-Parent};
p.Children.Add(c1);
p.Children.Add(c2);
x.Parents.Add(p);
x.SaveChanges();
那不是我想要的,所以我试图添加到父对象和上下文对象。
var x = new MyContext();
var c1 = new Child {Name =Alpha-Child};
var c2 = new Child {Name =Beta-Child};
var p = new Parent {Name =Alpha-Parent};
p.Children.Add(c1);
p.Children.Add(c2);
x.Children.Add(c1);
x.Children.Add(c2);
x.Parents.Add(p);
x.SaveChanges();
将子记录都存入数据库,但只有一个(第一个)被关联与父记录。
最后但并非最不重要的是,我每次调用Parent.Add()之后,都调用SaveChanges()。
var x = new MyContext();
var c1 = new Child {Name =Alpha-Child};
var c2 = new Child {Name =Beta-Child};
var p = new Parent {Name =Alpha-Parent};
x.Parents.Add(p);
p.Children.Add(c1);
x.SaveChanges();
p.Children.Add(c2);
x.SaveChanges();
但是,这打破了我对工作单位模式如何工作的想法。我应该能够调用SaveChanges()一次,并且所有的更改都会发生,正确吗?
那么我做错了什么?
以下代码将保存在单个保存更改中,并始终使用transactionscope单独保存。
public class Child
{
public int Id(get; set;}
public string Name {get; set; }
public Parent Parent {set; get;}
}
var x = new MyContext();
var c1 = new Child {Name =Alpha-Child};
var c2 = new Child {Name =Beta-Child};
var p = new Parent {Name =Alpha-Parent};
c1.Parent = p;
c2.Parent = p;
x.Parents.Add(p);
x.SaveChanges();
I am practicing with a code-first set of classes with a parent-child relationship between them.
public class Parent
{
public int Id ( get; set; }
public string Name { get; set; }
public List<Child> Children { get; set; }
public Parent()
{
Children = new List<Child>();
}
}
public class Child()
{
public int Id ( get; set; }
public string Name { get; set; }
}
public class MyContext : DbContext
{
public DbSet<Parent> Parents { get; set; }
public DbSet<Child> Children { get; set; }
}
In a simple console app I create a parent and create two child records and add them to the parent. If I then call the SaveChanges() only the first child is added to the children table.
var x = new MyContext();
var c1 = new Child { Name = "Alpha-Child" };
var c2 = new Child { Name = "Beta-Child" };
var p = new Parent {Name = "Alpha-Parent"};
p.Children.Add(c1);
p.Children.Add(c2);
x.Parents.Add(p);
x.SaveChanges();
Well that was not what I wanted, so I tried to maybe add then to both the parent object and the context object.
var x = new MyContext();
var c1 = new Child { Name = "Alpha-Child" };
var c2 = new Child { Name = "Beta-Child" };
var p = new Parent {Name = "Alpha-Parent"};
p.Children.Add(c1);
p.Children.Add(c2);
x.Children.Add(c1);
x.Children.Add(c2);
x.Parents.Add(p);
x.SaveChanges();
That got both the child records into the database, but only one of them (the first) was associated with the parent record.
Last, but not least, I tried to do a call to SaveChanges() after each call to Parent.Add().
var x = new MyContext();
var c1 = new Child { Name = "Alpha-Child" };
var c2 = new Child { Name = "Beta-Child" };
var p = new Parent {Name = "Alpha-Parent"};
x.Parents.Add(p);
p.Children.Add(c1);
x.SaveChanges();
p.Children.Add(c2);
x.SaveChanges();
But, this breaks my idea of how the "Unit of Work" Pattern works. I "Should be able to call SaveChanges() once, and have all my changes take place, correct?
So, what am I doing wrong?
The following piece of code will save in single save changes. And always use a transactionscope to save in single batch.
public class Child
{
public int Id ( get; set; }
public string Name { get; set; }
public Parent Parent {set;get;}
}
var x = new MyContext();
var c1 = new Child { Name = "Alpha-Child" };
var c2 = new Child { Name = "Beta-Child" };
var p = new Parent {Name = "Alpha-Parent"};
c1.Parent = p;
c2.Parent = p;
x.Parents.Add(p);
x.SaveChanges();
这篇关于使用EF 4.1将父记录和多个子记录插入单个“工作单位”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!