entity framework - How to recreate migrations from scratch -
i upgraded ef6 alpha 1-3 ef6 beta 1. this meant had recreate migrations created using alpha version.
so tried roll migration created using ef5. hit error introducing foreign key constraint on table may cause cycles or multiple cascade paths. figure because had neglected fix down migrations when fixing migrations same problem. (should have read this before)
anyway, rather try fix trying reset migrations - described here. deleted migrations table in database , migration .cs files, in package manager enable-migrations -enableautomaticmigrations -force , add-migration initial
when tried run application existing database initialiser (which has automatic migrations false) failed because tried create tables there. changed initialiser database.setinitializer(new dropcreatedatabasealways<mycontext>())
this time got introducing foreign key constraint on table may cause cycles or multiple cascade paths problem again during initialisation
so changed cascadedelete: true cascadedelete: false in migration file
but still same error!
update 1 removed creation of 1 table in migration file got same error. there must kind of cache somewhere or it's picking file don't know or it's generating own migration in background
update 2 figured when using dropcreatedatabasealways ef must generate migrations , changing cascadedelete false in migration file wrong place it. should done in fluentapi. added line modelbuilder.conventions.remove<onetomanycascadedeleteconvention>(); onmodelcreating. , deleted initial migration file. ran application , correctly generated database. thought i'd cracked but....
i changed initialisation use original configuration file:
internal sealed class configuration : dbmigrationsconfiguration<sid2013context> { public configuration() { automaticmigrationsenabled = false; automaticmigrationdatalossallowed = true; } protected override void seed(etc.. } then ran application , reported model had changed. did add-migration update-database , migration file create database created.
the problem when run application tries run update (even though automaticmigrationsenabled = false). "there object named 'xxx' in database" problem again. there entry in migrations table not match name of configuration file.
if start work "code first migration" using existing database, can following:
- i run add-migration "initialmigrations".
- it explores existing database , make migration step.
temporarily delete contents of "initialmigrations" step:
public partial class initialmigrations : dbmigration { public override void up() { //... //... } public override void down() { //... //... } }i run update-database
- this creates appropriate entries in table __migrationhistory.
- restores contents of "initialmigration" go work on empty database.
that's it.
update: initializer
as understand it, 'dropcreatedatabasealways' delete , create database. can used first installation. if have working installation launchpad, erase data. why hazardous.
i based on article: coding.abel.nu/2012/03/… own initializer i'm using. i'll write.
the seed executed, method yields same result, works if not run installation, run upgrade.
public partial class myentities : dbcontext { static myentities() { database.setinitializer<myentities>(new validatedatabase<myentities>()); } } /// <summary> /// http://coding.abel.nu/2012/03/prevent-ef-migrations-from-creating-or-changing-the-database/ /// </summary> /// <typeparam name="tcontext"></typeparam> public class validatedatabase<tcontext> : idatabaseinitializer<tcontext> tcontext : dbcontext { public void initializedatabase(tcontext context) { if (!context.database.exists()) { throw new configurationerrorsexception( "database not exist"); } else { if (!context.database.compatiblewithmodel(true)) { //from: //http://stackoverflow.com/questions/11611322/ef-4-3-code-first-migrations-seed-per-migration var cfg = new migrations.configuration(); var migrator = new dbmigrator(cfg); ///last in db, (first, reverse order) var dblast = migrator.getdatabasemigrations().firstordefault(); ///last in app var applast = migrator.getlocalmigrations().lastordefault(); ///what missing var pendings = string.join(", ", migrator.getpendingmigrations()); throw new invalidoperationexception( string.format("the database ({0}) not compatible entity model ({1}). pending migrations: {2}", dblast, applast, pendings)); } } } } update: setup
installation i'm using this:
http://coding.abel.nu/2012/04/update-database-msi-custom-action/
Comments
Post a Comment