EF6 database First check for database schema changes - entity-framework-6

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.

Related

Do we need to add new version to core data if we add new tables into it?

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.....

Migration conflict in existing Databases - EF6

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

Add existing database view/table from object explorer to entity diagram

I have created a new asp.net 2012 mvc application. I am moving an existing web forms application over to the mvc model. The web forms application uses linq to sql, but since it's no longer going to be supported, I am going to use linq to entities.
Having never used mvc or linq to entities, I have been reading every article I can find.
I added the ado.net entity model and generated the diagram from my existing sql server 2005 database. I was having problems in the .edmx diagram with a relationship so I deleted the view expecting to be able to drag it back to the diagram.
I have looked all over on adding an existing database view/table to a diagram with no luck. I don't want to regenerate it, I spent a lot of time modifying relationships etc. in the current Diagram.
How do I add back a view/table from a database to an existing diagram?
Thank you,
I was afraid the update model from database option would change what I had done in the model, so I avoided it.
After hours of searching, I decided to try it and duh... it let me pick what I want to add.
Right click on your .edmx file and select Update Model From Database. Check what you want and that's it.

asp.net mvc model database change

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

Why does a Model First Entity Framework 4.0 model require a database before creation?

I am reading an article about Entity Framework 4.0 that states the following:
"The model's context menu has an option to 'Generate Database Script from
Model'. When you select this option you'll find that you do need to point
to an existing database. The script won't create the database itself,
just the schema, which means that you'll need to create the database yourself
in advance."
If the EF 4.0 designer generates SQL to clobber the existing database, why is an existing database first required ?
Thanks,
Scott
It will generate the tables/indexes etc. for you. You just need to supply an existing , empty, database.
You'd run in to a chicken and egg problem otherwise, as you'd have to e.g. provide a connection string pointing to an existing database to be able to connect to a database in the first place.
And while there might have been some options to have EF generate the database as well, it's likely not worth the implementation trouble. Just poiint EF at an existing but empty database.

Resources