0

My project is built using ASP.NET MVC and I've used Entity Framework 6 to access data from Sql Server database. I am following Database first approach for EF6. Frequently I need to make changes in my Database schema like making column nullable, changing column names etc. Now option 'update model from database' works fine in some scenarios while in other it gives errors and my Models are not successfully updated. I require clarification on below two queries:

  • What is the best way to update my Models when there are some changes in my DB schema?;
  • Are there some specific changes in DB for which EF fails to update Models?
Programmer
  • 128
  • 2
  • 11
  • *while in other it gives errors* - please elaborate: what did you change and which error do you see? – Gert Arnold Mar 11 '18 at 14:34
  • When I added a new column in DB, in that case update worked fine but when I renamed column name in table, I get following error message 'Property "propertyName" is not mapped.' and update is unsuccessful. – Programmer Mar 11 '18 at 14:48

2 Answers2

1

Why not simply start over each time, to avoid these issues?

ErikEJ
  • 40,951
  • 5
  • 75
  • 115
  • Did you mean to say that I should delete my existing .edmx file and generate new one each time whenever there are new changes? – Programmer Mar 11 '18 at 14:20
  • You should avoid EDMX files and use a code only solution, like "EF Reverse POCO template" – ErikEJ Mar 11 '18 at 14:51
  • This seems to be a good alternative to use. However, in the documentation at URL: (https://github.com/sjh37/EntityFramework-Reverse-POCO-Generator-UI), following message is written 'Try to keep your db contexts small (< 60 tables).'; So does that mean that this tool shouldn't be use in situations when Db consists of large number of tables greater than 60 tables?; If its true then in such situations with large number of tables how should I successfully update my Models in Database first approach whenever there are changes in database. – Programmer Mar 12 '18 at 07:05
  • You can still use the tool, it is more of a runtime performance advice – ErikEJ Mar 12 '18 at 16:27
  • 1
    @Erik I think the advice to use POCO generator is better than "simply" starting over each time. EDMX models tend to have many manual modifications in the class model so starting over is hardly ever a viable option. – Gert Arnold Mar 14 '18 at 08:22
0

EF has always had issues with synchronizing the model to the database, so much so that I personally choose to just delete the edmx files and start over (as already suggested).

I will add though, that due to the security requirements of the industry I work in, we no linger create a model on the tables themselves but always use SPROCS for all data requirements. Since we moved over to this, dropping a sproc from a model, saving it and then adding a new revised sproc seams to work. Also, using sprocs insulates the code from the underlying structure allowing some changes to be made to the underlying database without changing the signature of the data from the models point of view.

Ian
  • 114
  • 4
  • So in case when one is not using procedures, is deleting .edmx file and creating it each time a good approach to follow? – Programmer Mar 12 '18 at 10:20
  • It works for us. It is frustrating that EF has continued to have these long standing issues and we would prefer this to work properly. We now just accept that it dosn't always work properly when tables change and delete the EDMX and make a new one. As it creates partial classes, any code we have added in separate partial classes gets retained so we don't find it that great a deal. It is not 'good' we have to do this but we feel it is 'good' to work around things we cann't change with as little fuss as possible. Deadlines are deadlines and it is a poor worker that blames its tools :-) – Ian Mar 13 '18 at 21:29