SqlBulkCopy的和实体框架 [英] SqlBulkCopy and Entity Framework

查看:215
本文介绍了SqlBulkCopy的和实体框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的项目包括3个标准层:数据,业务和presentation。我想用数据实体为我所有的数据访问需求。的应用程序的功能的一部分将它需要将所有数据内的平面文件复制到数据库中。该文件是没有那么大,所以我可以用SqlBulkCopy的。我发现关于SqlBulkCopy类在.NET中使用的几篇文章。然而,所有的文章都使用数据表来移动数据来回。

My current project consists of 3 standard layers: data, business, and presentation. I would like to use data entities for all my data access needs. Part of the functionality of the app will that it will need to copy all data within a flat file into a database. The file is not so big so I can use SqlBulkCopy. I have found several articles regarding the usage of SqlBulkCopy class in .NET. However, all the articles are using DataTables to move data back and forth.

有没有办法使用的数据实体以及SqlBulkCopy的或我将不得不使用数据表?

Is there a way to use data entities along with SqlBulkCopy or will I have to use DataTables?

推荐答案

您需要将实体转换为IDataReader的或数据表。

You'll need to convert the Entities to a IDataReader or DataTable.

有一个小的辅助类,旨在协助有: <一href="http://archive.msdn.microsoft.com/LinqEntityDataReader/Release/ProjectReleases.aspx?ReleaseId=389" rel="nofollow">http://archive.msdn.microsoft.com/LinqEntityDataReader/Release/ProjectReleases.aspx?ReleaseId=389

There is a small helper class designed to assist with with: http://archive.msdn.microsoft.com/LinqEntityDataReader/Release/ProjectReleases.aspx?ReleaseId=389

编辑:MSDN链接被打破,中高音副本可以在这里找到: <一href="https://github.com/matthewschrager/Repository/blob/master/Repository.EntityFramework/EntityDataReader.cs" rel="nofollow">https://github.com/matthewschrager/Repository/blob/master/Repository.EntityFramework/EntityDataReader.cs

msdn link is broken, alt copy can be found here: https://github.com/matthewschrager/Repository/blob/master/Repository.EntityFramework/EntityDataReader.cs

然后你可以使用SqlBulkCopy的像这样:

Then you can use SqlBulkCopy like so:

var sbCopy= new SqlBulkCopy(connectionString);
sbCopy.DestinationTableName = "TableName";
sbCopy.WriteToServer(entitiesList.AsDataReader()); 

这篇关于SqlBulkCopy的和实体框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆