Program Tip

Entity Framework 6.0에서 마이그레이션을 비활성화하는 방법

programtip 2020. 11. 14. 10:58
반응형

Entity Framework 6.0에서 마이그레이션을 비활성화하는 방법


Entity Framework 6.0 rc1을 사용하는 "자동"마이그레이션을 무시하려고합니다. 내 문제는 지금이 기능을 원하지 않으며 애플리케이션이 실행될 때마다 모든 테이블을 생성하려는 모든 엔터티 로그를 볼 수 있다는 것입니다.

감사합니다.


이 시도:

internal sealed class Configuration : DbMigrationsConfiguration<YourContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }
}

최신 정보:

다음을 시도해 볼 수도 있습니다.

Database.SetInitializer<YourContextType>(new CreateDatabaseIfNotExists());

이를 app.config의 entityFramework 섹션에 넣을 수 있습니다.

<contexts>
  <context type="YourNamespace.YourDbContext, YourAssemblyName" disableDatabaseInitialization="true"/>
</contexts>

이 msdn 페이지는 Entity Framework 구성 섹션 에 대한 모든 정보를 제공합니다 .


통해 볼의 Web.config - https://msdn.microsoft.com/en-us/data/jj556606.aspx#Initializers

코드를 통해 (이상하게도 훨씬 간단한 대답)

public class MyDB : DbContext
{
    public MyDB()
    {
        Database.SetInitializer<MyDB>(null);
    }
}

또는 Global.asax.cs

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        // ...

        Database.SetInitializer<MyDB>(null);

        /// ...

    }
}

If you found this question hoping for a simple answer to disable migrations because you typed "Enable-Migrations" and now things aren't working the way you expected, like not running the seed method you thought it would run, then look in the solution explorer and delete the Migrations folder. That will stop the code from looking at the migrations config to find initialization code. To get the Migrations folder back, just run "Enable-Migrations" again.


The mistake I was making was to call Database.SetInitializer(null); too late (after the context had been initialised). The best way to ensure migrations are disabled is to make the above call for all your contexts in your application startup. I favor this approach over setting it in the app.config so I can use my container to locate my contexts and then construct a call.

var migrationsMethod = typeof(System.Data.Entity.Database).GetMethod("SetInitializer");
foreach (var contextType in allContextTypes)
{
    migrationsMethod.MakeGenericMethod(contextType).Invoke(null, new object[] { null });                            
}

Disabling the automatic migration can also be configured during the invoke of the enable-migrations command (which creates the Configuration class), using the EnableAutomaticMigration parameter with a value of false:

enable-migrations -EnableAutomaticMigration:$false -ContextTypeName FullyQualifiedContextName

The will create a Configuration class which sets the AutomaticMigrationsEnabled property to false, like in the answer above.


The EnableAutomaticMigration parameter of the enable-migrations command is mentioned in this article of the Entity Framework Tutorial page (however they use it with true which seems to be the default value).


Try this, Add this line in your MyContext class, this will be called before your MyContext constructor is called. This will stop creating the database as well as won't add tables into a connected database. Basically this line disables the default Code-First Database Initialization strategy which basically has a default strategy as CreateDatabaseIfNotExists.

static MyContext()
{
       System.Data.Entity.Database.SetInitializer<MyContext>(null);
}

참고URL : https://stackoverflow.com/questions/18667172/how-can-i-disable-migration-in-entity-framework-6-0

반응형