简体   繁体   English

EF6 从 DB1 向 DB2 添加一个具有相同结构的新行

[英]EF6 Add a new row from DB1 to DB2 with the same structure

I have two different databases that have a table each with the same exact structure.我有两个不同的数据库,每个数据库都有一个具有相同结构的表。 I would like to upload the registers retrieved from DB1 into DB2.我想将从 DB1 检索到的寄存器上传到 DB2。 I tried some methods using the same entity or converting it to a List object, as follows.我尝试了一些使用相同实体或将其转换为 List 对象的方法,如下所示。 The list of the registers from DB1 is generated properly, but how to insert it into DB2? DB1 中的寄存器列表是正确生成的,但是如何将它插入到 DB2 中?

object pendingRowsList = new object();     
   
//Get registers from DB1
using (var context = new DB1())
{
  pendingRowsList = context.table_local.OrderByDescending(x => 
  x.id).Take(numberOfRows).ToList();
}
 
//Upload registers into DB2
using (var context = new DB2())
{   
  for (int i = numberOfRows; i > 0; i--)
  {
     var newDbRow = new table_remote(pendingRowsList[i]); //that is what I am trying to achieve

     context.table_remote.Add(newDbRow);

     context.SaveChanges();
      }
}

A couple of things: for (int i = numberOfRows; i > 0; i--) should be for (int i = numberOfRows-1; i >= 0; i--) otherwise i will be greater than the length of your pendingRowsList right away and you will never get to 0 .有几件事: for (int i = numberOfRows; i > 0; i--)应该是for (int i = numberOfRows-1; i >= 0; i--)否则i将大于您的长度pendingRowsList马上,你永远不会到达0 Also, depending on the number of records, this method will either take a VERY long time or will not be possible to do all at once.此外,根据记录的数量,这种方法要么需要很长时间,要么不可能一次完成。 There are limits to the number of rows you can insert all at once with Entity Framework (or any ORM for that matter) If you are using Sql Server, the best way to do this is with direct INSERT and SELECT - something like this:您可以使用 Entity Framework(或任何 ORM)一次插入的行数有限制如果您使用的是 Sql Server,最好的方法是使用直接 INSERT 和 SELECT - 如下所示:

INSERT INTO dbo.MyTable (Field1, Field2)
SELECT Field1, Field2 FROM dbo.MyOtherTable
WHERE 1 = 1 --or other qualifying set of records

Alternatively you can use the SqlBulkCopy approach which is also blazingly fast.或者,您可以使用速度非常快的 SqlBulkCopy 方法。 There is a helper class for this linked in this answer with an example:此答案中有一个帮助程序类,其中包含一个示例:

https://stackoverflow.com/a/9162325/396005 https://stackoverflow.com/a/9162325/396005

Or, you can "roll your own" using something like this which leverages the ToDataTable() extension method from the MoreLinq package或者,您可以使用类似这样的东西“自己动手”,它利用了MoreLinq包中的ToDataTable()扩展方法

var dataTable = pendingRowsList.ToDataTable();
// Depending on your entity model you might have to use 
// something like this to get the actual fields you need to insert
// var dataTable = pendingRowsList.Select(r => new { r.Name, RelatedId = r.Related.Id, ... }).ToDataTable();
using (var bulkCopy = new SqlBulkCopy(connection))
{
    for (var c = 0; c < dataTable.Columns.Count; c++)
    {
        var col = dataTable.Columns[c];
        bulkCopy.ColumnMappings.Add(col.ColumnName, col.ColumnName);
    }
    bulkCopy.DestinationTableName = "MyTableName";
    bulkCopy.BulkCopyTimeout = 0;
    connection.Open();
    bulkCopy.WriteToServer(dataTable);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM