0

I am working on .NET 5 Web API in which I have several projects in one solution. One project is for only controllers, another one I have added as class library project for interfaces, third project is also class library project for models which I would need to create it from existing DB using EF core. Fourth project is again class library project where I will write data manipulation logic using EF core. As most of the online tutorials are using Code first approach so I am little bit stuck for my DB first approach. Since I am doing this 1st time. I have few questions.

  1. Are these projects structures right? If not please suggest ideal enterprise level solution design structure.
  2. I have added EF core related nuget packages in 3rd an 4th project. Now I want to create entities from existing DB tables. How I can create it to particular project. I am running a below command

Scaffold-DbContext "Server=.;Database=BookStore;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer

Its creating context classes in API project instead of DTO project. How can I create models in DTO project.

enter image description here

  1. I have few tables now. Going forward we will add new table, SPs in DB as per requirements. In that case how to refresh models without disturbing existing models and validations logic written for it.
Nilesh
  • 518
  • 1
  • 9
  • 26
  • [How do I ask a question?](https://stackoverflow.com/help/how-to-ask) says: "Write a title that summarizes the **specific** problem" You seem to have more than 1 (related) problems. – Luuk Jun 25 '22 at 07:08
  • To scaffold in the DTO project you must run the command prompt in the DTO folder, perhaps you were in the API folder? Once you have your model code automatically generated, learn how it works. If you do this, you can manually create new tables just by modifying your model code accordingly – Davide Vitali Jun 25 '22 at 07:27
  • @DavideVitali I am running the scaffold command through package nanager console by selection DTO project in solution explore. Still facing hard luck. – Nilesh Jun 25 '22 at 07:34
  • Install [EF Core Power Tools](https://marketplace.visualstudio.com/items?itemName=ErikEJ.EFCorePowerTools) and do that in right click on the needed folder. – Svyatoslav Danyliv Jun 25 '22 at 08:27

1 Answers1

1

Open the Package manager console and select DTO from the dropdown. Run the following command. Make sure your directory must be there to import models into

Scaffold-DbContext "Server=.;Database=PalletPal_LicenseStore;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir YourModelDirectory -Force

Kamran Khan
  • 1,042
  • 10
  • 21
  • Hi, It worked for question 2. Can you please help me for my other 2 questions please. Thanks – Nilesh Jun 25 '22 at 10:14
  • You don't have to import entities to DTO. You must import entities to the DAL layer. Your DTO must contain only the blueprint **view models** of your entities that must be used in the service layer and API project. – Kamran Khan Jun 27 '22 at 10:25
  • For updating your entities you must run the query it will be updated – Kamran Khan Jun 27 '22 at 10:25