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.
Related
I renamed a few tables and some columns. When I run the Add-Migration command, the migration generates code that drops the old tables and columns and adds ones with the new names. This results in losing the data they contained.
Since I don't want to lose the data, I want to edit the migration, removing the drop and add commands, and replacing them with rename commands.
But after I edit a migration, how do I apply that change?
If I run the Update-Database command, that applies it to the database. But not to the snapshot that Entity Framework maintains of my schema (stored in ApplicationDbContextModelSnapshot).
I need a way to incorporate my edits into the model. How can I accomplish this?
So, this is definitely the messy part of code first.
As far as the question asked, as GuruStron suggested, the only way I found to have a valid custom migration is to edit it such that the result is the same as what the original, generated migration produced. This keeps it up to date with the database snapshot. And running Update-Database will run your custom update code.
I think my biggest problem was that I had too many changes going on at once. After struggling with this for a while, I undid some of my changes and added them back bit-by-bit. Entity Framework will rename a table or column if it can figure out that the new name refers to the same column. If it finds many changes, it can't figure this out.
In the end, I had to customize the migration a little for a couple of columns that were being dropped (customized them to be renamed instead). But I was able to get Entity Framework to rename my tables and other columns.
The key: make small changes at a time and carefully review the migration before applying them to the database. If you need to customize the migration, do it such that the end result doesn't change.
You don't.
I suppose you are developing using a code first approach, since the question has this tag on it.
If you are using code first, you must change your models and let Entity Framework change the database schema for you.
Suggested reading:
Migration in Entity Framework Core
Entity Framework Core Migrations
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 am using Entity Framework code first for the first time in a production environment. Everything went fine until we got the DB up and had put some of the data in it and then to get some of the data we were importing from another location we had to change field lengths. So we made some of the fields nvarchar(99) instead of nvarchar(50).
That went fine and the application still worked but I knew I needed to change the data annotation or it would blow up later when it loaded and tried to save a too long field. When I did that the app blew up even though the model and the db are now matching. So I thought that it was the hash in the metadata table so I thought I'd be clever and make a new DB and take the hash from there and copy it. That did not work and in fact now I cannot get my app to connect to the test db that we have data loaded in at all.
I do not want to drop and recreate this database. I want entity framework to realize that the model and the schema do in fact match. Is there any way for me to do this? Also why did copying the metadata from a DB that entity framework created with this model not work?
Entity Framework Code First creates a EdmMetadata table and saves a hash of your Model classes in it. When you change something in the Model, the hash of the new Model classes doesn't match what's in the EdmMetadata table anymore, and the app should "blow up" at runtime. What you need to do to keep using the same database without dropping it, is to delete the EdmMetadata table. This way EF will not do that check and will try to proceed with the access to the DB.
Check this video tutorial (skip to 8:10 of the "When Classes Change" section).
Sorry I fixed this. Removing the metadata worked. But turns out I had updated to a more recent version of EntityFramework accidentally while trying to fix my problem and this more recent version expected different naming conventions for the Database. In any case recreating the many-to-many group person table with a script from a DB created by Entity Framework and deleting the metadata fixed the problem.
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).