Entity Framework: Regeneration of changed tables (key changes) - c#

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!

Related

How do you edit a database migration?

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

Entity Framework 6 Designer Errors and Issues

I've been experiencing a lot of weird things with the Entity Framework designer in visual studio 2015 Professional. If I try to updated the model from the database it will delete all of the mappings of existing entities and replace the mappings with only the one I updated. If I add a relationship to existing entities it will delete all existing entity classes. The only way to update anything in the designer is to write it all out in XML.
Am I doing something wrong or is there something wrong with the file integrity maybe? I've found workarounds that say I have to completely delete the model and redo it from the database every time if I want to make a change. Updating the Model from the database. Even with this the auto-mapping isn't correct.
It shouldn't be this difficult, that's why I'm lead to believe that maybe something in the files that is making it lose it's integrity after copying it from the previous developer. Or is something else maybe the culprit?
What fixed it was instead of deleting the entire model from the project, I just deleted the Entities from the designer and recreated the entire model from the database. What threw me off when I tried this was it brought in some new fields that weren't in the model before and some table names had changed.

Adding a new table to EF

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.

How to get entity framework to realize the model and the DB are in sync

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.

How to update my entity framework

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

Categories