如何使用Entity Framework一次执行多个SQL命令? [英] How can I execute multiple SQL commands at one time with Entity Framework?

查看:75
本文介绍了如何使用Entity Framework一次执行多个SQL命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

var sql = System.IO.File.ReadAllText("data_tables.sql");
context.Database.ExecuteSqlCommand(sql);

我的data_tables.sql脚本包含这样的SQL语句:

My data_tables.sql script contains SQL statements such as this:

SET IDENTITY_INSERT Application ON
INSERT INTO Application(ApplicationId, Name) VALUES (1, 'xxxx');
SET IDENTITY_INSERT Application OFF
DBCC CHECKIDENT( 'Application', RESEED )

除了从文件中读取之外,还有一种方法可以将它们放入类并执行命令。我正在寻找的是一种一次性执行所有命令的方法。

Rather than read from a file is there a way I can put these into a class and have the commands execute. What I am looking for is a way to execute all the commands in one go.

推荐答案

存在实体框架可以简化操作您可以将数据从数据库中获取到UI并返回,以及使用LINQ或扩展方法查询数据。如果您的SQL语句列表与此有关,则进行某种转换可能会比较有利。但是,如果您只是执行预先准备好的SQL语句的干燥列表,那么我认为您当前的设置没有问题。

Entity Framework exists to make it easier for you to get data from your database to your UI and back, as well as query data using LINQ or extension methods. If your list of SQL statements has something to do with this, then it might be advantageous to do some sort of conversion. If however you're just executing a "dry" list of preprepared SQL statements, then I see no problem with your current setup.

这与您要执行的操作有很大关系,很难在没有更多信息的情况下提出策略。

This has a lot to do with what you're trying to do and it's very hard to suggest a strategy without more information.

这篇关于如何使用Entity Framework一次执行多个SQL命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆