简体   繁体   English

如何使实体框架在一次数据库访问中插入条目

[英]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: 因此,日志看起来像这样:

插入SmallEntities的日志

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: 该库支持所有主要提供商,包括:

  • Oracle DevArt Oracle DevArt
  • Oracle DataAccess Oracle数据访问
  • Oracle DataAccessManaged Oracle数据访问管理

This library allows you to perform all bulk operations you need for your scenarios: 该库使您可以执行方案所需的所有批量操作:

  • Bulk SaveChanges 批量保存更改
  • Bulk Insert 批量插入
  • Bulk Delete 批量删除
  • Bulk Update 批量更新
  • Bulk Merge 批量合并

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.

相关问题 实体框架:如何查询数据库中的多个相关表进行单次旅行 - Entity Framework: How to query a number of related tables in a database making a single trip 实体框架:一次往返执行多个命令 - Entity Framework: Execute multiple commands in one round trip 强制实体框架在一次往返中更新几条记录。 - Force Entity Framework to update several records in one round trip. 如何使用Entity Framework提高数据库插入性能 - How to increase database insert performance with Entity Framework 实体框架中数据库条目的版本历史 - Version History of Database entries in entity framework 实体框架更新数据库表中的条目 - Entity Framework to update the entries in the database table 实体框架:搜索具有空列的数据库条目 - Entity Framework: search for database entries with null columns 如何在数据库插入和更新上调用函数? 还使用实体框架吗? - How to call a function on database insert and update? Also using Entity Framework? 如何确保使用Entity Framework 5插入幂等数据库? - How do I ensure an idempotent database insert with Entity Framework 5? 如何告诉实体框架忽略数据库生成用于插入或更新的值? - how to tell Entity Framework to ignore database generates values for insert or update?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM