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??
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.
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.
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.
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);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With