如何通过C#运行我的实体设计器DDL脚本?

时间:2012-05-02 15:04:20

标签: c# sql-server entity-framework tsql ef-model-first

我尝试了几种方法来通过C#运行我的Model-First Entity Designer SQL文件,但没有运气。我有一个SQL文件,其中包含我加载和读取的所有代码。

以下是我的代码。 在执行的每个命令上都会出错。请注意,我正在通过我的Model Container的连接属性检索连接,该连接属性的类型为DbConnection,但我不确定它是否与它有任何关系。

C#脚本:

var commandStrings = Regex.Split(Resources.DatabaseScript, "^\\s*GO\\s*$", RegexOptions.Multiline);

//container is my EDMX container.

container.Connection.Open();

var command = container.Connection.CreateCommand();
var transaction = container.Connection.BeginTransaction();
command.Connection = container.Connection;
command.Transaction = transaction;

foreach (string commandInput in commandStrings)
{
    var commandString = commandInput;
    if (commandString.Trim() != "")
    {
        Debug.Write("Executing SQL ... ");

        try
        {
            command.CommandText = commandString;
            command.Connection = container.Connection;
            command.CommandType = CommandType.Text;
            command.Prepare();
            command.ExecuteNonQuery();
            Debug.WriteLine("Success!");
        }
        catch (Exception exc)
        {
            Debug.WriteLine("Failed!");
            Debug.WriteLine("Exception: " + exc.Message);
            Debug.Write("Rolling back ... ");
            try
            {
                transaction.Rollback();
                Debug.WriteLine("Success!");
            } catch(Exception exce)
            {
                Debug.WriteLine("Exception: " + exce.Message);
            }
        }
        finally
        {
            Debug.WriteLine("SQL: " + commandString);
        }
    }
}
transaction.Commit();
container.Connection.Close();

收到错误。我收到的一些错误如下:

错误1:

IF OBJECT_ID(N'[dbo].[FK_UserStory]', 'F') IS NOT NULL
ALTER TABLE [dbo].[StorySet] DROP CONSTRAINT [FK_UserStory];
  

查询语法无效。接近标识符'OBJECT_ID',第1行,第4列。

错误2:

IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
  

查询语法无效。接近标识符'SCHEMA_ID',第1行,第4列。

2 个答案:

答案 0 :(得分:1)

我不确定具体问题是什么,但是进行异常处理和事务控制的代码顺序有点奇怪,所以我在下面做了一些修改。如果这对您有任何影响,请告诉我。

var commandStrings = Regex.Split(
    Resources.DatabaseScript,
    "^\\s*GO\\s*$",
    RegexOptions.Multiline | RegexOptions.Compiled);

// container is my EDMX container.

container.Connection.Open();
try
{
    using (var transaction = container.Connection.BeginTransaction())
    {
        try
        {
            foreach (var commandInput in commandStrings.Where(commandInput => !string.IsNullOrWhiteSpace(commandInput)))
            {
                Debug.Write("Executing SQL ... ");
                try
                {
                    using (var command = container.Connection.CreateCommand())
                    {
                        command.Connection = container.Connection;
                        command.Transaction = transaction;
                        command.CommandText = commandInput;
                        command.CommandType = CommandType.Text;
                        command.ExecuteNonQuery();
                    }

                    Debug.WriteLine("Success!");
                }
                finally
                {
                    Debug.WriteLine("SQL: " + commandInput);
                }
            }

            transaction.Commit();
        }
        catch (Exception exc)
        {
            Debug.WriteLine("Failed!");
            Debug.WriteLine("Exception: " + exc.Message);
            Debug.Write("Rolling back ... ");
            try
            {
                transaction.Rollback();
                Debug.WriteLine("Success!");
            }
            catch (Exception exce)
            {
                Debug.WriteLine("Exception: " + exce.Message);
            }
        }
    }
}
finally
{
    container.Connection.Close();
}

}

答案 1 :(得分:0)

我解决了这个问题。我的代码运行得很好。

但不是通过container.Connection使用ModelContainer的连接,而是使用(container.Connection as EntityConnection).StoreConnection as SqlConnection