Scaffold-DbContext to different output folder

前端 未结 5 1362
日久生厌
日久生厌 2021-01-11 13:02

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.

相关标签:
5条回答
  • 2021-01-11 13:29

    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

    0 讨论(0)
  • 2021-01-11 13:33

    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

    0 讨论(0)
  • 2021-01-11 13:35

    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

    0 讨论(0)
  • 2021-01-11 13:42

    I faced a similar problem. I had my EF models in a separate project. In order to write the models there, I just used the following command:

    Scaffold-DbContext "Server=[Server];Database=[Database Name];Trsted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -Project "[Project Name]" -Force 
    

    The key for me was the -Project "[Project Name]" -Force option.

    As the top answer indicates, if you want a different folder within that project, you can just use the -OutputDir option.

    0 讨论(0)
  • 2021-01-11 13:44

    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.

    0 讨论(0)
提交回复
热议问题