In a class library Ado.net Entity Data Model is has generated POCO classes. These were generated fine for the first time. But database changes are not being reflected. In edmx diagram right clicking and choosing Update Model from Database show newly created table but it do not add table even after selecting it to add.
I tried running .tt (by right click and Run custom tool) but even it did not regenerated the Poco classes as per latest DB changes.
Help please
Not a fix but a workaround: Is it not an option to simply remove and regenerate the EDMX and the generated classes? That's what I do, it is much easier than working with the update feature, and the result seems to be the same. Your POCO extensions still remain the same and functional.
I use database first and I have my SQL upgrade scripts, the generated EDMX and my Generated models in source control and the changes there are very easy to manage. Here is a rough outline of my DB upgrade process for each version:
Create .sql script for the upgrade, statements like CREATE TABLE etc.
Delete generated files: Model.Context.tt, Model.tt, Model.edmx
Remove Entities string from Web.config (if you use it)
Create the EDMX and Context files the same way you did for the first time
If you use source control (I hope you do!) check what has changed
Test
Commit!
In my case i needed to save ModelName.edmx, then classes were generated.
Ensure that connections string in app.config is correct. I was using a DataDictionary and my connection string had the following path:
data source=|DataDirectory|*.sqlite
Thus, it wasn't updating. Because this DataDirectory variable was being resolved at runtime.
Related
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..
I need to upload a project from Visual Studio to Azure, but I have to share the same DB with other projects. So I guess I need to rename all my models that creates the tables in the DB to prevent any conflict with other projetcs model/tables!?
I started to change the name of one the model and then continued to change in the code and also in IdentityModels.cs file, but will this really work? Do I need to change in other files also? One way to simplify it all, would be to use search and replace, but then I have no control of what is changed!
Any good advice how to replace name of table in DB and all instances?
i think there is a migration plan if you change your model after creating the data base
link :
https://msdn.microsoft.com/en-us/data/jj591621.aspx
Using Entity Framework 6 Code First in an ASP.NET project with Visual Studio 2013, is there any way to rebuild (or update) the generated DataContext and model classes without stepping through the Entity Data Model Wizard every time?
I'm fully aware of how to do this with an EDMX designer, but again, I'm using the "Code First from database" method and just wondering if there's a one-click (or one console command) way to trigger the rebuild without having to delete the generated context class and then step through the Entity Data Model Wizard every time I make a change to the backing database.
In VS 2015 (and supposedly 2013/2012) you can use the Entity Framework Reverse POCO generator to accomplish this.
https://visualstudiogallery.msdn.microsoft.com/ee4fcff9-0c4c-4179-afd9-7a2fb90f5838
You can make all your changes to database first, and to re-generate your models all you have to do is save your Database.tt file (usually I just add white space).
Code first requires you to create the DataContext by hand. You don't create/change the database and refresh the DataContext class. The Code first from database or EF Reverse POCO template is a middle ground between true Code First and the Database First approach of doing things. It meets at the middle by generating the same kind of POCO classes that you would have written by hand in Code First. They don't do it in Code First way but the end result is something similar to Code First. Hence the confusing name.
If you are using one of these templates to generate POCOs, you can right click on the t4 template file and click Run Custom Tool. If that doesn't work, you might want to delete that entity and then run the custom tool again. Also right clicking on the EDMX and clicking 'Update Model from Database' should work.
I have the solution for rebuild without wizard:
using the t4 is Transform All T4 Templates
and create the classes
I have added added Entity Framework to my project and selected Code first from database when creating my models. But the problem is, i couln't find how find how to update existing models and add new models to my project when i make changes on database.
It is very straightforward. Switch on migrations, change/add you classes, create a migration and update your database. See here for example. There are plenty of other sources.
When you change your DB you can run the EF generation again but it will overwrite the existing files so you will lose any changes. If you want to maintain code outside the generated files then you can use partial classes. Or alternatively just code them by hand after the initial auto generation, it's quick once you get used to it! :)
Now that i have generated 19 code-first classes from an existing database by:
Creating a Model-First Entity, and choosing "Generate from database"
Right clicking and choosing "Add Code Generation Item"
EF 4.x DbContext Generater
All the classes i want are generated, and I'm very happy with them...
And now for my question(s)
Why are they all the generated .cs files nested in a sealed .tt collection?
Why can't i copy paste them out to any folder i like, and treat them like normal classes?
Is it just me, or are you unable to use migrations "update-database" when you do it like that?
I guess my overall question is, why are they located in a .tt folder?
And how shall i update my database now? By editing the database manually and then update the Model, and then generate the dbContext again? Or is there a trick to get .cs files out of a .tt folder?
How do you modify you database after generating your DbContext?
Why are they all the generated .cs files nested in a sealed .tt collection?
Your code-first classes are not code-first classes. They were generated based on your database schema. Hence, this is a database-first approach. With database first, your entity classes are generated based on the database. In other words, the database comes first, the code comes second. The .tt file is a T4 template that decides how to generate your classes based on the database. You could change the database and then regenerate your entity classes.
Why can't i copy paste them out to any folder i like, and treat them like normal classes?
Because they are generated files, based on the T4 template.
Is it just me, or are you unable to use migrations "update-database" when you do it like that?
This part of your question I can't answer for certain, and will have to defer. However it seems to me like you want to mix and match approaches. It sounds like you want to start by generating code classes based on your database, and from there forward, change the database based on new entity classes you add to the model project. Is that right?
You can forgo the code-generation part and just go with a pure code-first approach, now that you have an idea of what the entity POCO's should look like. I have tried this T4 code generation before (pre EF 4.1) and immediately abandoned it, because as you have seen, since the entity and DbContext classes are generated, you lose control over the object-oriented model.