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

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

问题描述

我目前的项目包括3个标准层:数据,业务和演示。我想使用数据实体来满足我所有的数据访问需求。应用程序的一部分功能将需要将平面文件中的所有数据复制到数据库中。该文件不是很大,所以我可以使用SqlBulkCopy。我已经找到了关于在.NET中使用SqlBulkCopy类的几篇文章。但是,所有的文章都使用DataTables来回移动数据。

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一起使用数据实体,或者我必须使用DataTables? / p>

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

推荐答案

您需要将实体转换为IDataReader或DataTable。

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

有一个小帮手类旨在协助:
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链接被破坏,可以在这里找到alt副本:
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天全站免登陆