1

I am using Database First with ASP.NET Core 2 and Entity Framework. When I initially use scaffold-dbcontext -connection.... in the Package Manager Console, all my classes are generated as expected and my context is created with the various modelBuilder.Entity methods.

If I make a change to my database, and I attempt to keep my Database First designing, and I rerun scaffold-dbcontext, my context is overwritten. This may not matter much as the models shouldn't change too much, but for example, I created a foreign key in a table that I already scaffolded. When I got EF to scaffold it my context only had those two new Entities (using -force). All my old entities were gone.

I had to rescaffold everything -force to get back everything.

Should I be scaffolding to a separate -outputdir and copy and paste to the original context and class? Don't I need all the Entities in the context to use them in my controllers?

johnny
  • 19,272
  • 52
  • 157
  • 259
  • 2
    The generated classes are partial so you can create another partial that is not overwritten: https://stackoverflow.com/questions/40344526/entity-framework-core-database-first-update-after-initial-scaffold – Steve Greene Feb 21 '18 at 19:37
  • I wrote the author there, but I do not understand how to implement what you are saying. How do I create partial class with the scaffolding? – johnny Feb 21 '18 at 20:38
  • As Steve said, classes are partial. My EF Core Power Tools remembers your table selection – ErikEJ Feb 21 '18 at 20:44
  • EF Core is code first. The reverse engineering is meant to be a 1 time thing to get your existing database in sync. I suppose you could hack together some partial classes and get it working, but you are swimming upstream on that. Better to just drop back to EF6 if you need an EDMX. – Steve Greene Feb 21 '18 at 21:24

0 Answers0