How to set the isolation level for Entity Framework CodeFirst Migrations

后端 未结 2 442
被撕碎了的回忆
被撕碎了的回忆 2020-12-28 17:59

If you run an entity framework migration (either automatic or explicit) against tables published for SQL Server replication you get the following error:

2条回答
  •  执念已碎
    2020-12-28 18:47

    You can write and execute SQL in your migration code:

    Before SQL Server 2012 use dynamic SQL:

    public override void Up()
    {
         Sql("DECLARE @SQL NVARCHAR(4000) = 'ALTER DATABASE '+ DB_NAME() +' SET ALLOW_SNAPSHOT_ISOLATION ON' ; EXEC sp_executeSql @SQL;", true);
    }
    

    For SQL Server 2012 or later:

    public override void Up()
    {
         Sql("ALTER DATABASE CURRENT SET ALLOW_SNAPSHOT_ISOLATION ON",true);
    }
    

    Change "ALLOW_SNAPSHOT_ISOLATION" to your isolation level.

提交回复
热议问题