How to disable migration in Entity Framework 4.3.1?
Asked Answered
A

3

39

Is there any way to disable migration in Entity Framework 4.3.1? I removed the migrations folder from the project and the generated tables in my database, but it doesn't work! How can you remove the migration?

Artois answered 14/3, 2012 at 14:23 Comment(5)
What do you mean by it not works?Gleaning
I mean when I delete these file and table, I ecpect to migration disabled, but it is enable yet. when I run the project it occurs an error about migrationArtois
Try to add Database.SetInitializer<YourContextType>(null) to startup of your application.Gleaning
It works. Thank you. But now, how can I create database with ef?Artois
For EF6 see How can I disable migration in Entity Framework 6.0Xanthochroism
G
40

If you don't want to use migrations but in the same time you want EF to create database for you, you just need to set correct database initializer:

Database.SetInitializer<YourContextType>(new CreateDatabaseIfNotExists<YourContentType>());
Gleaning answered 14/3, 2012 at 20:3 Comment(6)
new CreateDatabaseIfNotExists<YourContentType>()Moisture
What class/file does this actually go in?Albescent
@jep, the entry point for your application. E.g. global.asax.cs or Program.Main.Lundell
This does not work in EF6. MSFT turned it off because of conflicts between the migration and the intitializers. See entityframework.codeplex.com/workitem/1689Biracial
How to make it ALWAYS rebuild from scratch even if there was no changes? I tried many things to do so.Duppy
In aspnet.core 2.0 I get the error: 'DatabaseFacade' does not contain a defininition for 'SetInitializer'.Bullyboy
C
35

Deleting the Migrations folder has worked for me. I don't get any errors, it puts me back to where I started.

Carcinogen answered 21/12, 2013 at 17:49 Comment(1)
The Migrations folder where? I'm EF6 code-first and don't seem to have one anywhere. I certainly didn't make one.Gezira
I
4

The way that I got around this was to make sure that I turned off Automatic Migrations in my code:

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

and then I deleted the _MigrationHistory table from the database (this is usually created as a system table if you can't find it)

Indefensible answered 13/3, 2013 at 3:40 Comment(3)
Down vote for being so vague. Where exactly in your code did you include that command?Knit
hmmm... good question JBeckton. it's been a while since I've looked at that code, and I don't think that I still have access to it anywhere. I remember that it was in the Entity Framework setup section of my code. Not very helpful, I know, so my apologies on that.Indefensible
AutomaticMigrationsEnabled property is located in /Migrations/Configuration.csZollverein

© 2022 - 2024 — McMap. All rights reserved.