[英]How to make entity framework insert entries in one trip to database
I have an one-to-many relationship defined with EntityFramework code-first. 我有一个使用EntityFramework代码优先定义的一对多关系 。 Something like: BigEntity that contains a SmallEntitiesList (list o of SmallEntity).
类似于:BigEntity,其中包含SmallEntitiesList(SmallEntity的列表o)。
Whenver I update the SmallEntities list of that object, and I perform a dbContext.SaveChanges()
, I can see in the SQL logger that Entity Framework inserts those items by making a roundtrip to database for each one . 每当我更新该对象的SmallEntities列表并执行
dbContext.SaveChanges()
,我都可以在SQL记录器中看到Entity Framework 通过对每个对象进行一次数据库往返来插入这些项目 。
So the log is looking something like this: 因此,日志看起来像这样:
Each of these inserts looks like this: 这些插入中的每一个如下所示:
DECLARE
updatedRowid ROWID;
BEGIN
INSERT INTO SOME_TABLE(...)
VALUES (...)
RETURNING ROWID INTO updatedRowid;
OPEN '' /* @outParameter */ FOR SELECT
SOME_TABLE
FROM SOME_TABLE
WHERE ROWID = updatedRowid;
END;
Is there a way to make entity framework to behave differently and make these inserts making a trip to database for each one? 有没有一种方法可以使实体框架表现出不同的行为,并使这些插入物每个都进入数据库?
UPDATE: Already tried BulkInsert (there is no support for Oracle DevArt, which is what I am using). 更新:已经尝试过BulkInsert(不支持Oracle DevArt,这是我正在使用的功能)。
if you are using EF(version 6) 6.1.3 then you can achieve this with EntityFramework.BulkInsert extension provided . 如果您使用的是EF(版本6)6.1.3,则可以通过提供的EntityFramework.BulkInsert扩展来实现。 It will insert the list of object in single call which will improve application performance very much .
它将在单个调用中插入对象列表,这将大大提高应用程序性能。 Form more information check this out .
形成更多信息,请查看。
https://efbulkinsert.codeplex.com/ https://efbulkinsert.codeplex.com/
foreach(var item in ListItems)
{
context.Entry(item).State = System.Data.EntityState.Added;
context.Add(item);
}
context.SaveChanges();
OR 要么
foreach (var item in objectList)
{
context.YourObject.AddObject(item );
}
context.SaveChanges(SaveOptions.None);
OR 要么
Research - BulkInsert(); 研究-BulkInsert();
Disclaimer : I'm the owner of Entity Framework Extensions 免责声明 :我是Entity Framework Extensions的所有者
This library support all major providor including: 该库支持所有主要提供商,包括:
This library allows you to perform all bulk operations you need for your scenarios: 该库使您可以执行方案所需的所有批量操作:
Example 例
// Easy to use
context.BulkSaveChanges();
// Easy to customize
context.BulkSaveChanges(bulk => bulk.BatchSize = 100);
// Perform Bulk Operations
context.BulkDelete(customers);
context.BulkInsert(customers);
context.BulkUpdate(customers);
// Customize Primary Key
context.BulkMerge(customers, operation => {
operation.ColumnPrimaryKeyExpression =
customer => customer.Code;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.