I have a code-first entity model in EF5. But I want to manage the database changes manually -- I do not want EF to modify my existing database and all its data. But when I make parallel changes in the EF mapping and in the database, EF refuses to operate properly telling me I need to use code first migration. How do I turn this off?
set the Database.SetInitializer to null.
public class DatabaseContext: DbContext
{
//the base accepts the name of the connection string provided in the web.config as a parameter
public DatabaseContext()
: base("DatabaseContext")
{
//disable initializer
Database.SetInitializer<DatabaseContext>(null);
}
__MigrationHistory
doesn't have the latest migration, and you don't want to do it anyway. In my case, I use migrations in dev environment, but when I deploy to production, I use SSDT to update the database. Therefore, EF would complain the model has changed because __MigrationHistory
would not have the latest migration, but I can guarantee the database is updated. –
Disordered Database.SetInitializer
from the constructor to the class constructor. That ensures the call is only made just once. –
Philippi So the most complete answer that I have found is this:
- Delete
Migrations
folder inside your project. - Set
Database.SetInitializer<DatabaseContext>(null);
inside your DatabaseContext initializer. - Delete the table
__MigrationHistory
inside your database. For EF6+ the table is located underTables
but for earlier versions it is located underSystem Tables
. - Build and run.
- Profit.
__MigrationHistory
every time my application starts-up which adds a few more milliseconds to my app's startup time. Is there a way to disable the __MigrationHistory
check entirely? –
Matte If you want to completely turn off migrations:
https://mcmap.net/q/88314/-how-to-disable-migration-in-entity-framework-4-3-1
However, I found it better to keep code first migrations enabled, but use the -Script
option to have EF create a DB change script for me that I can apply to each database (development, QA, Production) manually:
Update-Database -Script -ProjectName MyProject -StartupProjectName MyProject
That way EF will create the change script for me, and I still have full control over changes being applied. I version the change scripts like any other source code.
completely turn off migrations
. To do that: add Database.SetInitializer<YourContextType>(null)
to startup of your application –
Meli If you already used Migrations then changing only Initializer won't help. You need to go to Management Studio, open your database tables, go to System Tables
folder and remove __MigrationHistory
table that is located there (for EF6 and above, it's located directly under Tables
). This will disable Migrations for good.
System Tables
. –
Khoisan sp_rename
. I also disabled the initializer. –
Drambuie I just resolved this "issue" by
- Deleting table "_MigrationHistory" from the database.
- Deleting "Migrations" folder form the project.
- Updating EDMX file.
- Clean project & rebuild it.
The config of my environment is following
1. Visual Studio 2017 15.8.2
2. ASP NET MVC project
3. .NET Framework 4.6.1
4. Entity Framework 6.2.0
_MigrationHistory
table automatically? –
Dollar © 2022 - 2024 — McMap. All rights reserved.