1

如何使用实体框架将一个数据表的记录添加到另一个数据表?使用数据集如下:

    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);
    }

如何使用实体框架执行上述操作?提前非常感谢:)

4

2 回答 2

0

假设您有两个表及其对应的 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();
于 2013-03-11T11:58:28.747 回答
0

另一种选择 - 使用名为 Source 和 Destination 的 POCO 类;

context.Destination.Prop1 = Source.Prop1
context.SaveChanges();

另外-(如果我可以访问它,我会将其添加为评论:))您需要使用 context.SaveChanges(),而不是 context.Save()

于 2013-03-11T12:16:03.377 回答