Database in use error with Entity Framework 4 Code First

后端 未结 6 662
借酒劲吻你
借酒劲吻你 2020-12-02 09:39

I have an MVC3 and EF 4 Code First application, which is configured to change the DB when the model changes, by setting the DB Initializer to a DropCreateDatabaseIfMod

6条回答
  •  清歌不尽
    2020-12-02 10:29

    I found in EF 6 this fails with an ALTER DATABASE statement not allowed within multi-statement transaction error.

    The solution was to use the new transaction behavior overload like this:

    context.Database.ExecuteSqlCommand(TransactionalBehavior.DoNotEnsureTransaction, "ALTER DATABASE [" + context.Database.Connection.Database + "] SET SINGLE_USER WITH ROLLBACK IMMEDIATE");
    

提交回复
热议问题