54

If I do any changes to my EF 5.0 model, VS does not seem to see the changes. I have tried adding a new table, which shows up fine in the model, but then if I try to use it somewhere the table does not show up in intellisense and I can't use it.

I have also tried changing datatypes of a column, which again shows up fine if I look at the .edmx model file, but visual studio will still give me compiler errors on that field because I am trying to assign the incorrect datatype.

If I delete the EDMX completely, and recreate it, then the changes work fine. but it is tiresome to regenerate from database each time when I should be able to just "update model from database" and rebuild.

I have never had these problems with EF before. Does anyone know the cause or the fix to this problem?

Thanks.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Kyle
  • 32,731
  • 39
  • 134
  • 184
  • 1
    What is the code generation strategy for your edmx? (Open the edmx, hit F4) – Heather Nov 19 '12 at 19:42
  • 1
    @Heather It is set to 'None'. I tried changing it to 'Default', thinking that must be the problem, and I now have dozens of erros saying The Type [table] already contains a definition for [column] for each table/column I have. Any thoughts? – Kyle Nov 19 '12 at 19:45
  • 1
    Yea, you've solved the first half of your problem. "None" will not generate code at all. Now that you're generating code you need to resolve the logical inconsistencies between your database and your edmx. In general there are two ways to do this: the first is to delete every entity out of your edmx and then run an update from database. – Heather Nov 19 '12 at 19:49
  • @Heather I can't seem to find why I'm getting the errors. As far as I can tell all of the fields are only defined once, in the .cs file it generates. – Kyle Nov 19 '12 at 19:58
  • It's almost as if there are two of the exact same .cs files somewhere, but I can only file one, and i'm only getting one set of errors. – Kyle Nov 19 '12 at 20:02
  • Open the edmx, select all, delete to remove all entities. Clean and build. Does it build cleanly at this point? If not, you haven't removed all of the code generation artifacts. If so, then run update from database on your empty edmx. One issue I had with the edmx generator in the past is that if sometimes created a second Designer.cs file (i.e. MyEdmx2.Designer.cs) with duplicate class definitions. Look to see if this has happened to you. – Heather Nov 19 '12 at 20:03
  • @Heather I removed all entities and cleaned and built. I now have two errorsm one in my context.cs that says partial declarations must not specifity different base classes, and one in my designer that says Models.Entities already defines a member called Entities with the same parameter types. There is only one designer.cs class. Thank you for your help and patience, by the way =) – Kyle Nov 19 '12 at 20:10
  • Ok, this is EF5 specific stuff that I don't see in EF4. I'd spend some time tracking down which partial classes are attempting to base from different classes and understand why. Without more background on your project (is it new, or does it have an established set of entity classes?) it's hard to give guidance. – Heather Nov 19 '12 at 20:19
  • Can you check if this is not this problem: http://stackoverflow.com/questions/12734333/entity-framework-5-update-model-from-database-does-not-generate-table-class/12736291#12736291. There is a bug in VS2012 where code may not regenerated if nested under edmx. – Pawel Nov 19 '12 at 22:00
  • Did you report changes to SSDL, CSDL and C-S content sections on your edmx ? – Eric Bole-Feysot Nov 21 '12 at 13:18
  • Using VS 2019 still face this problem. – Aryan Firouzian Nov 18 '19 at 13:20
  • @AryanFirouzian Have you tried this answer as a workaround for now? https://stackoverflow.com/a/19130577/1308743 – Kyle Nov 18 '19 at 17:02
  • @Kyle, I tried that one as well, and it didn't work last night. But when I built it in the morning the class was updated. I believe it has something with version control which locks class file, and it needs vs restart to release it. – Aryan Firouzian Nov 19 '19 at 07:47

11 Answers11

74

First Build your Project and if it was successful, right click on the "model.tt" file and choose run custom tool. It will fix it.

Again Build your project and point to "model.context.tt" run custom tool. it will update DbSet lists.

csharpsql
  • 2,190
  • 1
  • 19
  • 24
Mahdi Shahbazi
  • 1,063
  • 1
  • 10
  • 13
  • 1
    Thanks so much for this! This bug seems to have come back in VS2013 Update 3, but following this tip is a quick and easy work-around. – Patrick Montelo Jan 06 '15 at 21:20
  • Thank you so much. I had to first show excluded items then reinclude the .tt's in order to run the tool. – Kurtis Cochrane Nov 29 '16 at 21:08
  • Fix my problem too here with VS2019. It seems to be related to the fact that I pressed no the a previously "potentially harm" warning box I think – Simon Dugré Oct 19 '19 at 02:45
16

This is apparently a bug in the Entity Framework that the model does not get updated when your Edmx file is located inside a folder. The workarounds available at the moment are:

  1. Install VS 2012 Update 1 which should fix the bug.
  2. If you are not in a position to install Update 1, you will have to right click on the model.tt T4 template file and click run custom tool. This will update the classes for you.

Hope that helps someone out there.

Link: http://thedatafarm.com/blog/data-access/watch-out-for-vs2012-edmx-code-generation-special-case/

Ghlouw
  • 1,440
  • 16
  • 19
16

If this is the bug with the edmx file located in a folder it is now fixed - download and install VS 2012 Update 1. You can get it from: http://www.microsoft.com/visualstudio/eng/downloads#d-visual-studio-2012-update

Pawel
  • 31,342
  • 4
  • 73
  • 104
  • had the same problem, installed the update and it worked after a refresh of the table in the edmx. Version 4.4 of EF, it was about time they fix this! – Amc_rtty Dec 01 '12 at 23:27
  • Just heads up. Does not work when multiple solutions with EF are open. Must close em all down in order for VS to catch changes. (even if there are completely separate projects.) – Don Thomas Boyle Jun 18 '15 at 18:42
  • 2
    I have VS 2013 Update 1, But same bug happens. – Mahmood Jenami Apr 06 '16 at 07:21
13

I also had this problem, however, right-clicking on the model.tt file and running "Custom tool" didn't make any difference for me somehow, but a comment on the page Ghlouw linked to mentioned to use the menu item "BUILD > Transform All T4 Templates." which did it for me

Kristian
  • 336
  • 3
  • 7
8

Right click the .tt file and select "Run Custom Tool", that should update it: see image

Tshilidzi Mudau
  • 7,373
  • 6
  • 36
  • 49
7

You should have a <XXX>Model.tt file somewhere which is the T4 template that generates your model classes.

If it is in a different project, it will not update when you save the edmx file.

Anyway, try right-clicking on it in Solution Explorer and choosing Run Custom Tool

Nick Butler
  • 24,045
  • 4
  • 49
  • 70
  • Also the other way around: if you choose `Run Custom Tool` without having saved the edmx file, the entities will not be updated. – bvgheluwe Jan 20 '14 at 10:06
3

I searched for this answer because I had a similar situation in VS2013. In my case, I found that a simple "Clean Solution" cleared out all the old definitions.

woodvi
  • 1,898
  • 21
  • 27
1

Are you working in an N-Tiered project? If so, try rebuilding your Data Layer (or wherever your EDMX file is stored) before using it.

Middas
  • 1,862
  • 1
  • 29
  • 44
  • the edmx is in the same project as I am using it. I have tried commenting out complier errors rebuilding it anyways though, with no success. – Kyle Nov 19 '12 at 19:43
  • I would look into what Heather suggested on your question then. The Code Generation Strategy should be set to Default in your case. – Middas Nov 19 '12 at 19:47
  • I would also check your table and columns, make sure they are publicly accessible. – Middas Nov 19 '12 at 19:48
0

I've also experienced this problem with none of the classes being generated under the model.tt file. In my case it was down to issues with how I had built the DB in SQL2012. I'd set a column in a table to nullable that was also a foreign key and although I think you should be able to do this it caused a problem in EF5.

As soon as this was cleared and the diagram updated from the database they reappeared.

EF5 VS2013

CF5
  • 1,123
  • 9
  • 19
0

After Changing the laptop When I updating Entity Framework model, stored procedures are not seen in code

What I have done to make this working

  • Update visual Studio 2012 Update 5 (Not Working)
  • Update/Modify from Programs (Not working)
  • After update stored procedures, build the project again then go to context.cs, right click and run custom tool (Not Working)
  • Doing RND (Someone says the issue is due to some permissions in MSSQL) But unfournately I have done alot of permission in MSSQL but (Not Working)
  • Install 2019 and run the project. Now everything works fine

Conclusion

  • Visual studio 2012 have a bug (bug is resolved in Update 1, 2, 3 and 4)
  • Change the Visual Studio version will resolve the issue
Adil Shah
  • 21
  • 1
0

I had this problem in EF 6 using Visual Studio 2022. I tried all of the things here plus cleaning and rebuilding the solution, deleting the stored procedure out of EDMX and the database and re-updating, closing VS, and restarting my computer. None of those things worked. What ended up fixing it was simply opening the EDMX file. When I did that, Visual studio found the missing properties in the result object.

Hoodlum
  • 1,443
  • 1
  • 13
  • 24