[英]Update records with Entity Framework without loading in first
I'm looking for a way to write code with Entity Framework to update 1000's of records that match a criteria. 我正在寻找一种使用Entity Framework编写代码的方法来更新符合条件的1000条记录。
In SQL it would look like this 在SQL中它看起来像这样
UPDATE [Items]
SET [IsInSeason] = 1
WHERE [Name] like '%summer%'
It doesn't make sense for me to load in all items into memory just for this update. 对于我来说,只为此更新将所有项目加载到内存中是没有意义的。
And I would like to avoid writing regular SQL statements (if possible) 我想避免编写常规的SQL语句(如果可能的话)
Out of the box, Entity Framework has no such ability. 开箱即用,实体框架没有这种能力。 You either need to load the entities before updating, or you need to resort to raw SQL (as an ad-hoc statement, or by calling a stored procedure). 您需要在更新之前加载实体,或者需要使用原始SQL(作为临时语句或通过调用存储过程)。
There are a few EF extension packages, however, that claim to support this batch update and batch delete scenario. 但是,有一些EF扩展包声称支持此批量更新和批量删除方案。 I haven't had any personal experience with any of them, but give them a look: 我没有任何个人经验,但请看一看:
To work with Entity Framework I believe you have to load the data into memory. 要使用Entity Framework,我相信您必须将数据加载到内存中。 How else are you going to give your statements? 你怎么还要发表你的陈述? You could write a stored procedure that you call from your data layer if you really don't want to load the data into memory and just let SQL Server handle it (if you're working with SQL Server) 如果你真的不想将数据加载到内存中并让SQL Server处理它(如果你正在使用SQL Server),你可以编写一个从数据层调用的存储过程
You can get all items and update them in loop like this 您可以获取所有项目并像这样循环更新它们
(from x in dataBase.Items
where x.Name.Contains("summer")
select x).ToList().ForEach(xx => x.IsInSeason=1);
if you use entity framework ,you should get all items as objects , do the nessecar changes , and save them . 如果您使用实体框架,您应该将所有项目作为对象,进行nessecar更改并保存它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.