Scaffold-DbContext to different output folder

痞子三分冷 提交于 2020-04-10 08:24:48

问题


I'm implementing repository pattern in company solution I work for, separating model classes in a Backend project and database context and migrations in DbContexts project.

I'm using Scaffold-DbContext setting my Backend Project as default project to destination of model classes, however DbContext Class is always created in same folder as model classes. Is it possible to redirect the creation of the DbContext class to a different output folder, in my case to DbContexts project?


回答1:


It is now possible to redirect the generated context with -ContextDir option:

-ContextDir The directory to put DbContext file in. Paths are relative to the project directory.

So in your case it would be something like this:

Scaffold-DbContext "*connection*" "*provider*" -OutputDir "BackendProject" -ContextDir "DbContexts"

Source: https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell




回答2:


Yes, you can do that with my "EF Core Power Tools" free Visual Studio extension:

https://github.com/ErikEJ/SqlCeToolbox/wiki/EF-Core-Power-Tools




回答3:


You need to change the Default Project to the project which you want to generate entities in, also specify the folder (using OutputDir switch) where you want to generate the model definition. Example is below

Scaffold-DbContext "Data Source=.\SQLEXPRESS;Initial Catalog=DbName;Integrated Security=SSPI;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -f -d




回答4:


Short answer: no.

Currently, the output folder will contain the models and the context, and it's not possible to change their namespace either.

You can look here for the supported parameters.



来源:https://stackoverflow.com/questions/48282223/scaffold-dbcontext-to-different-output-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!