Entity framework 实体框架将一个数据表的内容添加到同一数据库中的另一个数据表中
如何使用实体框架将一个数据表的记录添加到另一个数据表中? 与数据集类似:Entity framework 实体框架将一个数据表的内容添加到同一数据库中的另一个数据表中,entity-framework,data-binding,entity,Entity Framework,Data Binding,Entity,如何使用实体框架将一个数据表的记录添加到另一个数据表中? 与数据集类似: private void sourceTabletransfer() { foreach (DataRow sourceTableRow in myDataSet.Tables["sourceTable"].Rows) { DataRow destinationTablerow = myDataSet.Tables["destinationTable
private void sourceTabletransfer()
{
foreach (DataRow sourceTableRow in myDataSet.Tables["sourceTable"].Rows)
{
DataRow destinationTablerow = myDataSet.Tables["destinationTable"].NewRow();
destinationTablerow["date"] = sourceTableRow["date"];
destinationTablerow["varchar1"] = sourceTableRow["varchar1"];
destinationTablerow["int1"] = sourceTableRow["int1"];
myDataSet.Tables["destinationTable"].Rows.Add(destinationTablerow);
}
this.destinationTableBindingSource.EndEdit();
this.destinationTableTableAdapter.Update(myDataSet);
}
如何使用实体框架执行上述操作?
提前非常感谢:)假设您有两个表及其相应的PoCo类。将它们命名为ParentEntity和ChildEntity
foreach(var parentEntity in lstParentEntities)
{
ChildEntity child=new ChildEntity();
child.prop1=parentEntity.Prop1;
child.prop2=parentEntity.Prop2;
child.prop3=parentEntity.Prop3;
context.AddObject(child);
}
context.SaveChanges();
另一种选择—
使用名为Source和Destination的POCO类
context.Destination.Prop1 = Source.Prop1
context.SaveChanges();
另外-(如果我有权访问它,我会将其添加为注释:)
您需要使用context.SaveChanges(),而不是context.Save()谢谢您的快速回答和解决方案。我没有想过使用POCO类。很简单,谢谢你的回答。如果你正在编辑大量的记录,请考虑在数据库中使用Update /INSERT语句。这将只需要对数据库发出一个请求,而不是对每个正在更新的记录执行n+1。使用EF(或任何客户端技术)比使用数据库内解决方案的性能要慢得多。谢谢您的提示。我还学到了一件事。以上是针对每次传输的少量记录,速度不是问题。不过,对于其他项目,我会记住你的建议。