Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I stop EF (Code First) Checking For Schema Changes

I have a problem that's causing hair loss...

I have a project build using Code First (EF); all is well and it works like a charm however, I cannot find a suitable way of allowing for Database changes (via .sql scripts) without the application throwing a tantrum at start-up because the schema has changed.

I have read and read into this problem, I have deleted the EdmMetaData table so it cannot compare the hash of the database but then read there was a bug with the EF in that if this is deleted, it still thinks the schema has changed (as it doesn't recognise the missing table, so it compares an empty string!).

Also, I have tried using the Database.SetInitializer(null) in the Global.asax file but this means I cannot access the Data Model later on.

Everybody talks about using the option of dropping the database if the schema changes etc.

This is what I'm looking for:

For the EF/Code First to do... NOTHING! Sweet FA! Nowt!

... If I change the schema via an external application, I want it to ignore the changes altogether and just ASSUME I have made the relevant model changes to work with these database changes, made via the .sql script. If I miss a column or table, then I accept my stupidity and I expect a tantrum then but, until I make a mistake, I want the EF to trust me.

This means I can easily update any of our client(s) with a .sql script, at any time and not worry about the EF going tits up! :(

ANY ideas guys??

like image 742
Jonny Avatar asked Mar 15 '12 08:03

Jonny


People also ask

How do I disable Code First Migrations?

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.

What is Entity framework code First migrations?

Code First Migrations is the recommended way to evolve your application's database schema if you are using the Code First workflow. Migrations provide a set of tools that allow: Create an initial database that works with your EF model. Generating migrations to keep track of changes you make to your EF model.

How to enable code First migrations?

Go to Package Manager Console and type command help migration. Type Enable-Migrations -ContextTypeName EXPShopContext. This command creates a migration folder with InitialCreate. cs and Configuration.


2 Answers

With more recent Entity Framework versions, you no longer need to do what Ladislav Mrnka says. Since EF 4.3, you just need to set the initializer for the WebContext to null. For instance:

Database.SetInitializer<FabrikamFiberWebContext>(null);
like image 186
Jason Crease Avatar answered Oct 12 '22 04:10

Jason Crease


In addition to setting initializer to null add also this to your context class:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    model.Conventions.Remove<IncludeMetadataConvention>();
}

It will turn off hash comparing.

like image 27
Ladislav Mrnka Avatar answered Oct 12 '22 04:10

Ladislav Mrnka