我在我的代码中将EF更新为EF 6.0.2,我有以下代码行:
applicationDbContext.Database .ExecutesqlCommand(@"ALTER DATABASE CURRENT SET RECOVERY FULL;");
更新后,我收到以下错误信息:
ALTER DATABASE statement not allowed within multi-statement
transaction.
我已经解决了一个TransactalBehavior的问题,如下面的代码:
applicationDbContext.Database.ExecutesqlCommand( TransactionalBehavior.DoNotEnsureTransaction,@"ALTER DATABASE CURRENT SET RECOVERY FULL;");
我的问题:
为什么我会收到这个错误与EF 6?
>我的修复是一个有效的修复问题或一个恶魔隐藏在这个解决方案后面?
>还有其他办法解决问题吗?
任何帮助将不胜感激!?
解决方法
EF 6更改了使用ExecutesqlCommand的事务
Starting with Entity Framework 6.0,ExecutesqlCommand() by default will wrap the command in a transaction if one was not already present. There are overloads of this method that allow you to override this behavior if you wish.
EF 5的行为方式不一样.你的修复是适当的
You can now specify a TransactionalBehavior flag to instruct EF on how to handle transactions with this command.
var sqlCommand = String.Format("ALTER DATABASE {0} SET SINGLE_USER WITH ROLLBACK IMMEDIATE"); db.Database.ExecutesqlCommand(TransactionalBehavior.DoNotEnsureTransaction,sqlCommand);
通过使用DoNotEnsureTransaction标志,在执行命令之前,EF不会启动事务.这样可以使ALTER DATABASE命令成功执行.
http://www.danbartram.com/entity-framework-6-and-executesqlcommand/