I have been updating my Entity Framework by simply right clicking and clicking on "update model from database". I usually go under the "Add" tab and then click the tables and click finish. I also use "refresh" sometimes as well. What are the differences between these? and also when I do refresh or add sometimes the entity comes out wrong or keeps some of the old information in cache, how can I just get the entity to match my database and clean out any of the old cached things.
Yes, you are right. The designer is a bit buggy and doesn't handle certain schema changes particularly well. As a rule, when I add a table to the database, I run Update model from database and select the table in the Add tab. Works fine. If I add a field to an existing table, I do Update model and Refresh. Works well too.
But if I add one field and remove another, or rename an existing field (as far as I remember), EF does not handle this correctly (added a new field but failed to remove the old one, and since the old one remained unmapped, the model validation failed). So in those more complex cases I usually delete the entity and then do Update model from database and Add it again.
The downside is that if you did make some changes (e.g. renamed some fields or navigational properties of the entity), you'll have to do those changes again.
It doesn't clean out old items. You have to delete those yourself. On the positive side, Visual studio will give you an error when you compile, stating that a coloumn or two isn't mapped. The entity framework tool is more of a code generator. It generates classes based on the structure of your database, as opposed to being tied to the database for is't structure information.
Designer is not buggy and the reason why it doesn't handle some changes is because CSDL and some MSL modifications made by user are not overwritten. CSDL is the diagram you see in designer and MSL is the mapping of your entities and associations.
Believe me this is a big step forward comparing to Linq-to-sql where no update existed and you always had to delete everything including your changes when you wanted to refresh your model. If you made a lot of changes you can always delete your entity in EF designer as well. EF designer offers a choice to update or recreate (by deleting the old one).
Related
For some reasons we need to update Views inside the Entity Framework.
So we followed this solution on another question and it worked like a charm!
BUT here is the problem:
if we update our model (for some new fields or tables/views) the complete Mappings are destroyed and after updating we get the Warning
Error 11007: Entity type 'UpdateView1' is not mapped.
After this it's not even possible to load the entities because all Mappings are lost.
So how to design the views to be able to post updates using the Views AND to be able to update the edmx file?
MS is dropping support for EDMX files going forward in Entity Framework - in part due to the difficulty of keeping the database, EDMX, and POCOs all sync'd, as you are experiencing. (Anyone can edit any of the 3, then changes are lost when a sync is done).
So they recommend using the Code First approach. Code First is a bit of a misnomer and causes some confusion.
Code First doesn't mean you have to start with code.
You can start with the database and then write your POCOs to match it (as in your case). Doing so allows you to preserve all your mappings and such because you just adjust your code to match the structure already present in your database.
Another misconception is that you have to use migrations. You do not. You are welcome to not enable migrations and manually edit both the database schema and POCOs as long as you ensure they remain in sync.
We are upgrading an old VB6 application which sits on a SQL Server 2005 database, to an Entity Framework solution. The database remains the same, except - we're adding a new table. Is it possible with Entity Framework, to maintain the existing structure, when it gets installed on a client PC - and just add one new table?
Is this how Code First will work? Can I be 100% certain that no other tables will be modified?
i don't think, the effort is worth it to switch to code-first if you have an existing database and want to add only one table.
it is possible to map code-first classes to an existing database (reverse engineer code first). actually, i'm not very experienced with that workflow, but i know you can. You have to deal with a lot of manual mapping (with DataAnnotations or Fluent API), so in your case i would recommend to use the Entity Framework Database First workflow, since adding a single table saves you a lot of work.
this link has some useful information: Arthur Vickers Blog - Don't use Code first by mistake
You have two options, use a database editor such as SQL management studio to create the table which you can then map to a ef entity, or use migrations for ef which will let you update your database via ef.
Take a look at the migrations tutorial here: http://msdn.microsoft.com/en-gb/data/jj591621
I am using the database first approach, since a database developing team is doing the changes I require in the database on the SQL server for me.
Hence, I have to update the EDMX whenever the schema in the database changes.
Note: Changing one single table directly does not work for me, because VS doesn't always detect the changes right (for this issue, here are some details in SO if you're interested).
Hence, I am using the following workaround (regenerating all the tables):
In VS 2012, open the EDMX file by double-clicking on it. The graphic representation of the tables is shown.
Left-Click into the EDMX designer, then select all tables by pressing CTRL+A. Then, remove them by pressing DEL.
Right-Click into the EDMX designer and select "Update Model from Database ..." in the context menu.
The Update Wizard opens. In the "Add" tab, check "Tables", and depending on the requirements, check "Pluralize or singularize generated object names", "Include foreign key columns in the model" and optionally "Import selected stored procedures and functions into the entity model". Usually, I am using the "Pluralize..." and "Include foreign key columns..." options.
Click Finish. Now Save by pressing Ctrl+S.
That workaround works fine for me, and requires just a minute to update the model reliably.
Basically, I have a Sql Server database whose schema changes. When this happens, I have to update the EF data model. This is fine if all I did was add or delete a table: go to the designer, find "Update Model From Database..." from one of the multiple locations it exists and go through the wizard.
Unfortunately, the wizard doesn't just let me replace the whole model from the database. It can also only do one thing at a time. So if I make the unfortunate decision of making multiple schema changes and even worse, forget what I did: I have to do multiple steps of adding, refreshing, and deleting tables from the model.
This is obviously cumbersome. So for a lack of a better procedure, I have to blow away the model and go through all the steps of recreating it from the database. Crap: I left the connection string in the configuration file. Now I have to go delete that and start the wizard over or else it won't generate the same entities class name and now all of my code will break.
Why can't this just blow away the model for me and generate from the database? More importantly, why hasn't anyone else asked this question? What are people doing?
If you want to blow away and replace the model, the easiest built-in way is to just delete it and re-create it. However, if you have made any customization of the model you will of course lose that too.
As a workaround, to allow continuous incremental schema changes (and changes applied on both ends) I wrote a utility that compares the database to the SSDL layer of the EF model, and the SSDL to the CSDL layer, display the diffs and allow individual (or all) differences to be synced across.
You can see it in action here: http://www.youtube.com/watch?v=doqYOlcEAZM ...and if you want to try it out you can download it from http://huagati.com/edmxtools/
You could write a cmd-script that does it via command-line:
http://msdn.microsoft.com/en-us/library/bb896270.aspx
Your two requirements are in contradiction. You want to blow up whole model but in the same time you want to keep your customization. The first situation was possible in Linq-to-Sql designer but every change was lost every time you updated the model. For EF Designer MS decided to use second approach where designer almost never touch anything already existing in your CSDL spaces (entities) but changes only storage model and you must modify model little bit (in designer) manually after some breaking changes. I used both designers and have to say that productivity and usability of the second approach is much better. That is also reason why people usually don't complain about this.
Those who complain usually do either:
Buy some tool (like a great one referenced by #Kristofer) because MS completely skipped anything related to merging changes on different level of granularity and its not going to be better in the near future
Write a script or custom tool which will put all changes back every time they delete whole model (that was a way we used with Linq-to-sql)
Don't use designer and maintain XML manually
Go to code mapping instead of EDMX
The situation: Sometimes a database schema is not what you would consider an ideal representation of the system's information and you may not be able to change it. We have been using Entity Framework to create a nicer conceptual model to code against in situations like this. This means updating the model from the database and then changing it ourselves, either through the designer or through the .edmx file directly using a text editor.
The problem: When you update the model from the database, all your carefully made changes are thrown out the window. This can make adding new entities a real hassle as you are basically forced to do it through editing the .edmx file directly.
The question: Is there a way to get the Entity Framework to only update selected entities from from the database? Or is it possible to tell it to leave the rest of the model alone when adding a new entity?
Thanks!
No there is no way to make selective updates with built-in designer. Also the designer doesn't throw away all your changes. It usually doesn't touch conceptual model (except some rare occasions where it continuously renames some associations) and mapping but it always deletes storage model and override it with new definition. I worked without any problem with modifications to my conceptual model and mapping and running updates from the database.
Designer works as any other in Visual Studio - touching the generated code (storage model) is not supported feature. Once you do it you cannot use Update from database anymore.
There is commercial tool which probably supports better model updating - you can try a trial.
If by updating selected entities, you mean just one or more tables, you can delete those tables from the model, and then add them back in individually to pull in changes tables by choosing them individually - I do that often as underlying tables are changed (especially during development).
You do end up losing any manual changes you made to those re-added entities after the entity/table was pulled into the model (i.e. I often rename my navigation properties and then after each re-import of the table I need to manually rename them again).
I've got a pretty large DB hooked up to my Entity Framework. I've got one particular set of tables where a common key (fk) was deleted from the design. When I "update the model from the database", I get a series of errors about this now-missing key.
Why doesn't the update process accurately detect that this key was removed? Also, if I manually remove the property from the model, I just end up with other errors. I also tried to simply delete the table and re-add it, but now the table doesn't show up in the "Add" section of the update wizard.
So, short of deleting the entire thing and starting over, is there any way to recover gracefully? Has the EF team released any newer versions of this wizard that has some of these issues fixed in it already?
Entity Framework does get confused from time to time. What you need to do is delete the Entity Table from the model, then rename the table in SQL Server to TableName_Temp, refresh the model, now the Entity Table should be OK, so delete the entity table again rename it back to the original name and refresh the model once more. This time everything should be fine.
I know this is a messed up procedure but this is the only way I found to fix this issue without blowing away the entire edmx and starting from scratch. Currently at my organization we have a policy not to make changes to the EDMX unless absolutely necessary so that we can delete and recreate when ever needed without any problems.
Hope this helps!