update model from database not updating code - entity-framework

I have a model in my website which was generated from a database. When I right click on the edmx diagram to update the model when I have made changes in my database (using mySQL), the model doesn't update the vb code file with any new tables and properties. I am using the standard .tt templates. Isn't the code supposed to regenerate every time the model is updated in this manner?

Related

Update Model From Database EF Core/.NET Core

I have reverse engineered our model from an existing database using EF Core Scaffold-DbContext. Since then a couple of tables have been added to the database within SSMS and I now need to update the model. How can I do this without overwriting the original model classes (changes have been made to them)?
You should not make changes to the generated code, they are generated as partial classes, so you can make another file with your changes, and then simply re-generate the files.

EF6 Updating database from model overwrites my DefiningQuery

I'm using DefiningQuery to specify a Read only "view" entity that contains fields from two tables, and it works great for my need.
But each time I'm updating my model (using the Model designer) and runs the "Generate Database from Model..." operation, my DefiningQuery is lost because the .edmx file is getting overwritten.
The SQL being generated (in the .edmx.sql file) now assumes my Read Only entity must have a corresponding table created.
Is there a way to avoid the DefiningQuery being overwritten when the model is updated?
If possible I would like to avoid having to generate a Database View and map to that.
A little info:
I'm working on a project based on the Entity Framework 6.x.
I'm using the Model First approach.
The database is an SQL Server.

Entity framework: Manually maintained mappings

I've been using EF for a while (4 with model first) and so far I've not created any mapping manually. Whenever I need more entities/tables, I add an entity and the associations (all foreign key) and click "update database from model", which, as is well known, doesn't update any database from the model (although it does need a database connection for reasons I don't know). What it does is generating a storage model and the appropriate mappings to it, which are all stored back to the same edmx xml file.
So far, that has always been enough for me but I'm wondering what the workflow would be if one is to tweak the mappings and storage model manually. "Update database from model" overwrites all manual customization - so how is one to fix most of the mappings and storage model? Because I clearly don't want to do it all by hand - in fact I couldn't even figure out how to actually create a table in the storage model other than by editing the edmx in the xml.
I have the same problem. I just use a mixture of methods. If I add a field to the database, I just add the field to the model file. If I do a major restructure, I delete the table and recreate it by generating it from the database. Sometimes, I actually edit the edmx as XML to change or add things. You just kinda gotta figure out what process works best for you. I have managed to avoid heavy customization in the edmx by using the T4 template or changing the database and regenerating.

How do you update an edmx file with database changes?

I have an edmx file and I changed a table in my database. I know that there is an "Update Model from database" wizard, however in many cases this is useless.
For example if I change a field from non null to nullable or if I remove fields the update model does not reflect the changes. I have had to remove the entity and add it back in to get the changes to appear in my model.
Per the following question:
How do I propagate database changes to my .edmx file?
One of the answers seems to say the same thing, that you need to remove the entity and add it back in.
Is this the definitive answer or is there a better way to do this?
Updating an EDMX the safe way:
As you have found, the update from database does not always change existing properties correctly.
From our day-to-day use of EDMX updating (100s of updates over 24 months), I would recommend the following sequence for updating an EDMX.
Delete existing model and then update:
Open the EDMX designer
Ctrl-A to select all
Delete key to delete all models in the designer
IMPORTANT: Do not save the EDMX at this point if you are using TFS for source control!*
Now right-click and select "Update Model from Database" to recreate the entire model again.
Rebuild project to propagate changes
This will obviously lose any manual tweaks you have made to the model, but manual tweaks are to be avoided if possible. This makes the entire process reproducible at any time (which is a good thing).
Important notes:
If you have auto-save turned on in Visual Studio, you need to select the update (step 5 above), quickly to avoid an auto-save saving everything.
If you are using TFS for source control, and happen to save the EDMX after emptying it, TFS will mark all the generated files as "deleted" and updating the EDMX again can result in disconnected files that are not in source control!.
This process will not update any stored procedures. Further, I have found that a refresh of an EDMX will also not update stored procedures where just the return type has changed (still current as of EF 6.1.1).
Additional Recommendation:
Keep your EDMX in a separate library. This also becomes a great place to add additional TT files and partial classes (e.g. to extend function of EDMX models). I also place any extension methods for the database context in this library. The migration files get generated in the library too keeping it all nicely contained.
Update April 2015
The latest Release 4 of Visual Studio 2013 appears to have resolved a lot of the TFS issues. We now see Visual Studio checkout generated files, then revert them if they are unchanged. The above steps still appear to be the safest approach.
Update September 2015
Using latest VS2013 Release 5, we still have issues if a save occurs during EDMX update. You can still wind up in a state where pending deletes causes your tt files to be removed from source control during the update. The secret is to update fast between steps 4 and 5! :)
An important first step is to understand exactly what happens when you use the update model wizard.
From the MSDN Library:
The ADO.NET Entity Data Model Designer (Entity Designer) uses the Update Model Wizard to update an .edmx file from changes made to the database. The Update Model Wizard overwrites the storage model as part of this process. The Update Model Wizard also makes some changes to the conceptual model and mappings, but it only makes these changes when objects are added to the database. For example, new entity types are added to the conceptual model when tables are added to the database, and new properties are added to entity types when columns are added to a table. For details about what changes are made to the .edmx file, see Changes Made to an .edmx File by the Update Model Wizard.
When you updated the database using the update model wizard, it updated the storage model in the .edmx file and not the conceptual model. When changes are made to the definition of existing objects, only the storage model is updated; the conceptual model is not updated. For a complete description of changes that are made by the update model wizard, please see the "Changes Made to an .edmx File by the Update Model Wizard" link above.
Here are some options on how to update objects that are not updated by the update model wizard (based on your scenario where a the column definition was altered):
Use the update model wizard (to update the storage model), open the .edmx file using the designer (default), find the desired scalar property and edit the desired properties in the Properties windows.
Use the update model wizard (to update the storage model), open the .edmx file using the XML editor, find the desired property in the CSDL (conceptual model) section and change the desired attributes. This is basically the same as option 1, but you're editing the XML directly (a find and replace might be useful here).
From the Model Browser, delete the desired entity from the Entity Types section of the conceptual model and the desired table from the Tables / Views section of the storage model. Then use the update model wizard to add it back.
The best option would depend on the given scenario. For example, if you just altered the definition of one column, then option 1 is likely you best choice. If you altered the definition of a number of columns in a single table, then option 3 might be your best choice. If you altered a column that is used across a number of tables (such as a primary / foreign key), then editing the .edmx XML directly might be your best option.
Consider I have added a new column(c1) to my existing table. Then to update the same in my existing Entity Model, I would do the following.
I will open the .edmx file in notepad ++.
I will add the property c1 to .edmx file where ever necessary. For example I would add c1 node below every c0 node.
<EntityType Name="table">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
<Property Name="c0" Type="nvarchar(max)" />
<Property Name="c1" Type="nvarchar(max)" />
</EntityType>
Reload the project in Visual studio.
And finally add the attribute c1 to the model.
Step 1:
Double click on .edmx file. (Diagram Window will open)
Step 2:
On the diagram window, right click and select Update model from database... (Now it will update only in update storage but not in model)
Step 3:
Right click on Model.tt file and click on Run Custom Tool (Now it will be updated in model too)
That's It!
If I understand your question and your example, once you do the update model from database step and you're sitting there on the Model.edmx diagram, you can highlight the property in the class that you want to change and show the properties on it, and change the Nullable property for it to Nullable: True. That's at least one way to do this.
I believe the idea here is that conceptual model (that isn't being changed from non-null to nullable) can actually differ from the underlying database table and so it doesn't change that part and that difference might be exactly what you intend. The two ways I handle this are either doing the remove & add as you mentioned or more typically I manually set the properties as I mentioned.
Firstly, double click .edmx file
Secondly, Right Click on the Empty space and select "Update Model From Database"
Thirdly, Select Refresh tab on the menu bar.
Lastly, Select the table you want to refresh and select Finish..

Building DB schema from Entity Framework Model

I see that EF can update a model based on an existing database schema. However, I'm starting totally from scratch; I don't want to build tables, then rebuild them in the EF model file. Is there a way that I can draw out a model file, and have the SQL tables automatically be created for me?
Unfortunately, you have to wait for version 2 of EF.
Here is a link to the EF team's blog, where they talk about adding Model First support in v2:
One of the most painful omissions from the Entity Framework V1 was Model First, which basically means creating a conceptual 'model first' and then deriving a storage model, database and mappings from that.
[...]
The next release of the Entity Framework will include the ability to generate database schemas from your model. The main entry point into this feature is via the Designer context menu, to which we will add a new option called “Create Database from Model”.