문제

When migrating an Entity Framework 5.0 based project from code-first to database-first. We'll be using the standard VS2012 wizard to generate the edmx models off the database but do we have any additional steps beyond that? I presume I'd have to delete all classes where I've defined the code-first models and even the migrations folder - any other cleanup operations beyond those two?

[Edit]: Reporting back.

So the actual experience was between my original expectations and what Ladislav mentioned (like he said, exact conditions are code dependant). For me the entire operation took about 15-20 minutes mostly involving

  • Branch creation (safety, in case stuff blows up!)
  • Removal of Code-first classes and source (I moved them outside VS2012 for a diff reference)
  • Creation of the EDMX model from the database (passing same namespace etc to the wizard to reduce the extent of differences)
  • Quick inspection of the code-first classes and the auto-generated db-first classes. This was mostly 1:1 since we used sensible names in the code first model to begin with.
  • Compiling and fixing each error one by one
  • Noticed many errors were due to automatic pluralization of EF 5 vs my own pluralization of different fields. Fixed 40+ errors via a quick case sensitive search-> replace all
  • Rerun all the tests after compiling
  • Merge feature branch back in!

Thanks

도움이 되었습니까?

해결책

This may be quite complex if your entity or context classes contains any additional logic. You must:

  • Add EDMX model to your project and let it generate entity classes and context class
  • Remove migrations
  • If your original context class contains any additional code you must convert it to partial class with the same name, namespace and assembly as newly generated context class with only that additional code.
  • If any of your entity classes contained any additional code you must follow the same procedure as with the context.
  • If the additional code was actually called from inside of the code which is now auto generated you must make some other changes which may include changing code generation template and using partial methods.
  • You must remove any database initialization or migration execution from your bootstrapper
  • You must use the new connection string referencing metadata files

Another more complex option is not using auto generation feature and use your old context and entity classes but in such case you will have to keep them in sync with your EDMX manually.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top