如何使用实体框架编写插入(如果不存在则更新)?
我有多个字符串值,我想插入到 SQL Server 数据库表中,但我想一一检查值是否已存在于数据库中,我将更新,如果不存在,我将插入它。
我正在使用 Entity Framework 4.1,我希望能够以最佳性能做到这一点,这意味着尽可能减少对数据库的调用。
我之前看到过 这个问题,但他们没有使用 linq to sql实体框架。
I have multiple string values, I want to insert in an sql server db table, But i want to check values one by one if it already exist in the db I will update, if not I will insert it.
I am using Entity Framework 4.1, and I hope I can do that with best performance, means less calls to db as I can.
I saw this question before, but they are using linq to sql not entity framework.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一种方法是批量查询是否存在...例如,使用 .Contains 方法 (像这样),您可以查询可能存在或可能不存在的部分或全部项目。然后,一旦本地有了数据,您就可以在插入之前快速检查它是否存在
One way you could do it is to batch up the queries for existence ... for example, using the .Contains method (like this), you can query for some or all of the items which may or may not exist at once. Then once you have the data locally, you can quickly check if it's there before inserting