Entity Framework (ASP.NET): How do you update the column mapping after adding a column to the framework?
Asked Answered
B

4

11

I added a column to the Entity and then I go to generate the database from the model or build the solution and I get the message that the new column I just added is not mapped. The database was previously created from the model with no errors.

When I right click on the Entity and select Table Mapping, I can see the old columns there mapped but I don't see a way to m

Badenpowell answered 7/5, 2011 at 16:4 Comment(3)
code first? What does your model look like? Need more details to answer this questionFoeman
I haven't written any custom code. I created a Model and right clicked, Add Entity. I added columns to the generated GUI and then the database from there. Afterwards I went back to the model and saw GUI for the Entity/Table with all of the existing columns. I right clicked on it, Add, Scalar Property, then typed in my new column name. Then I click outside the entity and select Generate Database from Model. That's when I get the mapping error. When I right click on the Entity and go the Table Mapping, I see the existing mappings, but I don't see a way to add a new column there.Badenpowell
have you found a solution to this problem? I am in the same situation nowLashelllasher
S
11

The only way I've found to resolve it without "update model from database" (what causes other issues in my legacy project) is to edit the .edmx file with the XML editor, add the property (Name and Type) to the EntityType and save. After that when I open the .edmx editor the added property is there for mapping.

Serviceberry answered 2/10, 2015 at 10:43 Comment(1)
For small changes, avoid all the headaches and directly update in the XML editor. For example, if you have changed varchar to nvarchar in DB then make those changes in the XML editor directly.Causalgia
H
5

The easiest way to resolve this issue is to right click on the Entity Model and choose "Update Model From Database". Then select the "Refresh" tab and find and select only the table you wish to map a column for. Click the Finish button and you should have everything properly mapped. If you notice a duplicate column in you Entity, just delete the one you previously created and rename the new column to your liking. It's pretty frsutrating that Microsoft doesn't let you manually add just one column mapping.

Hypochlorite answered 26/10, 2011 at 21:11 Comment(2)
this didn't work for me... the table mapping is missing some columns stillIntrospection
My issue is that the "Update from database" is broken. It crashes every time I attempt it. I haven't been able to use this reliably for the last couple of years. It's very frustating that there isn't some official way to manually update the mappings.Davie
C
1

If you are just trying update a single parameter, you can open the file Model.edmx and change this single parameter, but if is more than one parameter it's better the option "update from database". It's important note that table mapping can't be changed using VS.

Cymry answered 20/9, 2017 at 13:39 Comment(0)
D
0

Refreshing did not work for me as the other answers suggest, or editing the .edmx directly as xml. Delete the model and pull it again from the database (with "Add new").

Deflection answered 9/9, 2019 at 21:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.