How add tables to existing EF Core 3.0 context by PM console?

asp.net-core c# entity-framework-core

Question

I need to add new tables to my dbContext. Can I do via the package management console?

For clarity, this is my command for the scaffold:

PM> `Scaffold-DbContext "Server=dbServer;Database=myAppDb;User Id=UserAppdb;
Password=MyPass;" Microsoft.EntityFrameworkCore.SqlServer -o Models -t Users, Devices, 
Items -context "AppdbContext"`
1
0
2/7/2020 12:59:32 PM

Accepted Answer

Entity Framework Core encourages you to use the code-first approach and migrations (as mentioned in the comments to your question). The database-first approach is not really supported, but you can work your way around it:

I would suggest that you re-execute your scaffolding command in a new solution and then copy the new classes that represent the new tables to your original solution (you might have to adapt their namespaces) If your new tables have relations to any tables that previously existed, you'll have to adapt their corresponding classes as well by adding the navigation Properties. Then you have to add a DbSet for each newly added class in your DbContext. Then you have to modify the OnModelCreating() method in your DbContext (You can probably just copy the contents of the newly generated one.

Generally you should consider applying the code-first approach in the future, especially if you want to ship your application to customers. With code-first, you never modify the database directly, but you only modify your code by adding classes and registering them in your DbContext.

0
2/7/2020 2:05:02 PM

Popular Answer

you were close! You should specify -t for each table so in your case it will be:

`Scaffold-DbContext "Server=dbServer;Database=myAppDb;User Id=UserAppdb; Password=MyPass;" Microsoft.EntityFrameworkCore.SqlServer -o Models -t Users -t Devices -t Items -context "AppdbContext"`


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow