Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过C#运行实体设计器DDL脚本?_C#_Sql Server_Entity Framework_Tsql_Ef Model First - Fatal编程技术网

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

如何通过C#运行实体设计器DDL脚本?,c#,sql-server,entity-framework,tsql,ef-model-first,C#,Sql Server,Entity Framework,Tsql,Ef Model First,我尝试了几种方法通过C#运行我的模型第一个实体设计器SQL文件,但没有成功。我有一个SQL文件,其中包含我加载和读取的所有代码 下面是我的代码它在执行的每个命令上都给出一个错误。请注意,我正在通过模型容器的connection属性检索连接,该属性的类型为DbConnection,但我不确定这是否与此有关 C#脚本: 收到错误。我收到的一些错误如下: 错误1: IF OBJECT_ID(N'[dbo].[FK_UserStory]', 'F') IS NOT NULL ALTER TABLE [d

我尝试了几种方法通过C#运行我的模型第一个实体设计器SQL文件,但没有成功。我有一个SQL文件,其中包含我加载和读取的所有代码

下面是我的代码它在执行的每个命令上都给出一个错误。请注意,我正在通过模型容器的connection属性检索连接,该属性的类型为
DbConnection
,但我不确定这是否与此有关

C#脚本:

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

错误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列


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

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();
}

}

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

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();
}

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


但是,我没有通过
container.connection
使用ModelContainer的连接,而是必须使用
(container.connection作为EntityConnection)。StoreConnection作为SqlConnection
,我解决了这个问题。我的代码运行得很好


但是,我没有通过
container.connection
使用ModelContainer的连接,而是使用
(container.connection作为EntityConnection)。StoreConnection作为SqlConnection

没有解决问题,但是+1用于“finally”子句,并用于总体上稍微改进结构。没有解决问题,但是+1对于“finally”子句和总体上稍微改进结构。