A developer has enables migrations in a project with an existing database.
Sometimes we get exceptions because the classes don't reflect the tables.
Sometimes there is class properties not mapped to the db, sometimes there isn't even the property but there is a field in DB.
So we have to add some field that allready are are in the database, change another, etc. It causes a lot of changes that couldn't happen.
Currently I'm applying just the insert in migration table to skip the changes, but that isn't the best approach.
How to deal with this situations?
Thanks in advance
Related
I have an existing core data project now I want to add some more tables into it so my question is do we need to add change current version of CoreData and add new version to it ? Also my app is already live. Now I want to publish another version of application by adding some more tables to it.
https://developer.apple.com/documentation/coredata/using_lightweight_migration
Core Data Migration
In order to follow core data migration we should keep on versioning our .xcdatamodeld file. Also to address new changes we should not make any changes in existing data model. Instead we should create a new version of .xcdatamodeld and perform changes there. We should follow core data migration if there are changes in:
Entity -
Add a entity,
Remove a entity,
Renaming a entity.
Attribute-
Add an attribute,
Remove an attribute,
Renaming an attribute,
Relationship -
Add a relationship ,
Remove an relationship ,
Renaming an relationship .
It is better to make an explicit migration, especially if you have your app live and there is user data in the field.
Core Data has a method for it, the
Lightweight Migration. Core Data can typically perform an automatic data migration, referred to as lightweight migration. Lightweight migration infers the migration from the differences between the source and the destination managed object models.
If you want to adapt also parts of your old model, when they are affected by your changes you may use the Heavyweight Migration
There you may migrate the old user data and you can change your old model if it is affected by your changes and your changes are logically complex.
Nevertheless if there are no changes which affect your old model you may also only add a table, but I personally migrate.....
I have a model.edmx file generated using ef6 database first approach.
The database is used by a number of apps and I have no control over when or how the schema may change.
There is a GUI wizard to "update model from database" which works for my purpose.
The wizard detects differences between my model and the database, so if I add a table directly to the database, or add/remove a column from some existing table I can see those changes in the "update model from database" wizard.
Is there a way to detect these changes programmatically? and is it possible to update the schema programmatically? or just regenerate it altogether to match the database?
At the least, is it possible to programmatically distinguish that the model is out of sync with the db, using existing EF libraries, rather than writing my own custom schema compare.
Thank you.
Setup
I have an app that uses ASP.NET Identity 2.0. The identity part shares a database with the rest of the tables needed by the application. So in one class library, I have a dbcontext that accesses the database for business data, and in another class library, I have the IdentityModel.cs, ie, the ApplicationDBContext.
Problem:
All worked fine, until I got in a muddle, trying to figure out how to work with migrations with the business data context. I ended up deleting the __MigrationHistory table and hence all the model metadata in the database for both the context.
I now get the following error:
Model compatibility cannot be checked because the database does not contain model metadata. Model compatibility can only be checked for databases created using Code First or Code First Migrations.
I deleted all migrations related to the business data, and re enabled migrations for that project. That had the effect of recreating the model metadata for the business dbcontext.
However, I can't figure out how to achieve the same for the Identity metadata.
Question:
How do I recreate the model metadata in __MigrationHistory for ASP.NET Identity 2.0?
Write below code in Global.asax.cs and try again...
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<TsContext>());
I had to recreate the database and start again. I am still in the dark to the extent that I don't know if Neel's answer is the correct one for a production database. Right now, I don't have the time to properly research AspNet.Identity to find the solution to the problem. Sorry.
Prevention before Cure:
What I DO know is that the problem would not happen if I hadn't deleted the __MigrationHistory table. So the issue is one of self harming and so prevention rather than curing is the best option.
Therefore, the moral of the story is:
Moral:
Never, EVER, delete the __MigrationHistory table. Learn a bit more about it BEFORE doing any thing quite so silly, feckless, carless and downright dumb.
I need to remove a column from the database in EF 4.3. The database has updated successfully, but the scaffolding is still there for the CRUD views and errors when trying to access them. Is there a way to auto remove scaffolding once the model changes, or do I have to manually delete all instances?
I'm not sure, since I don't know what scaffolding generator you're using. If it's the one that "comes with the box", I'm pretty sure you have to delete the CRUD manually.
The scaffolding generators I'm familiar with are just generators. They don't maintain an awareness of what's been generated, so they don't respond to data model changes. It's a a oneway process.
OTOH, that also means you can probably "eliminate" the scaffolding gunk by rerunning the generator. You'd have to save the customized bits you've added to the scaffolding output, of course, but that may be simpler than deleting what's no longer needed.
I've used mvcScaffolding and mvc3 to generate my tables in a sql2008 database. some data has been added.
I've changed a single model and wish to alter the underlying table.
is there a way to auto-update a single table to keep it in sync with the model?
after adding a new model how can the database table be created without recreating the database
You're looking for something like Rails' migrations. There are a couple .NET migrations providers floating around, but the basic setup you've described doesn't support alterations to the schema without recreating the database (at least in my experience).
have you tried treating your database an an existing one as described in this example by Scott Gu?
http://weblogs.asp.net/scottgu/archive/2010/08/03/using-ef-code-first-with-an-existing-database.aspx
Hopefully thias way you will not have to re-create your db everytime a change is required...
Regards
Paul