实体框架 – 实体框架中的更改数据库6

前端之家收集整理的这篇文章主要介绍了实体框架 – 实体框架中的更改数据库6前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在我的代码中将EF更新为EF 6.0.2,我有以下代码行:
applicationDbContext.Database .ExecutesqlCommand(@"ALTER DATABASE
 CURRENT SET RECOVERY FULL;");

更新后,我收到以下错误信息:@H_301_5@

ALTER DATABASE statement not allowed within multi-statement
transaction.@H_301_5@

我已经解决了一个TransactalBehavior的问题,如下面的代码:@H_301_5@

applicationDbContext.Database.ExecutesqlCommand(
TransactionalBehavior.DoNotEnsureTransaction,@"ALTER DATABASE CURRENT SET RECOVERY FULL;");

我的问题:@H_301_5@

为什么我会收到这个错误与EF 6?
>我的修复是一个有效的修复问题或一个恶魔隐藏在这个解决方案后面?
>还有其他办法解决问题吗?@H_301_5@

任何帮助将不胜感激!?@H_301_5@

解决方法

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.@H_301_5@

EF 5的行为方式不一样.你的修复是适当的@H_301_5@

You can now specify a TransactionalBehavior flag to instruct EF on how to handle transactions with this command.@H_301_5@

var sqlCommand = String.Format("ALTER DATABASE {0} SET SINGLE_USER 
                                WITH ROLLBACK IMMEDIATE");
db.Database.ExecutesqlCommand(TransactionalBehavior.DoNotEnsureTransaction,sqlCommand);

通过使用DoNotEnsureTransaction标志,在执行命令之前,EF不会启动事务.这样可以使ALTER DATABASE命令成功执行.@H_301_5@

http://www.danbartram.com/entity-framework-6-and-executesqlcommand/@H_301_5@

猜你在找的MsSQL相关文章