Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7903765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:49:57+00:00 2026-06-03T09:49:57+00:00

I tried several approaches to run my Model-First Entity Designer SQL file through C#,

  • 0

I tried several approaches to run my Model-First Entity Designer SQL file through C#, but with no luck. I have an SQL file with all the code in it which I load and read from.

Below is my code. It gives an error on every single command executed. Note that I am retrieving the connection through my Model Container’s connection property which is of the type DbConnection, but I’m not sure if that has anything to do with it.

C# script:

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

Errors received. Some of the errors I am receiving are as follows:

Error 1:

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

The query syntax is not valid. Near identifier ‘OBJECT_ID’, line 1, column 4.

Error 2:

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

The query syntax is not valid. Near identifier ‘SCHEMA_ID’, line 1, column 4.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:49:58+00:00Added an answer on June 3, 2026 at 9:49 am

    I solved the issue. My code worked just fine.

    But instead of using the ModelContainer’s connection through container.Connection, I had to use (container.Connection as EntityConnection).StoreConnection as SqlConnection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried several things so far, but I haven't had much luck yet.
I have tried several ways to login to a website through java. I have
Here is what I have. I have tried several things but I can't figure
All, I have read several threads on how to run subprocesses in python and
I've tried several of the solutions that I've found here, but none seem to
Sorry to ask this question again but I tried several solutions on stackoverflow and
I am stumped by this one. I have tried several angles and some come
I really need advice on how to do the following. I have tried several
I have perused all related threads on SO but still confused about how to
I have read several articles regarding templates for web site content. They all seem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.