SqlException: Syntax Error Near 'GO'

前端 未结 3 943
無奈伤痛
無奈伤痛 2021-01-17 11:06

I am having trouble sending a SQL statement through a DbContext using context.Database.ExecuteSqlCommand().

I am trying to execute

CREA         


        
3条回答
  •  猫巷女王i
    2021-01-17 11:42

    I know, necroposting is bad maner, but may be this post would save someone's time. As it was mentioned in Dave's post, GO is not a part of SQL, so we can create little workaround to make it work

                var text = System.IO.File.ReadAllText("initialization.sql");
                var parts = text.Split(new string[] { "GO" }, System.StringSplitOptions.None);
                foreach (var part in parts) { context.Database.ExecuteSqlCommand(part); }
    
                context.SaveChanges();
    

    In this case your commands would be splitted and executed without problems

提交回复
热议问题