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?
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://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell
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
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.
You can generate models and DBContext in different projects (not only different folders in the same project) like this:
Scaffold-DbContext "connectionString" Microsoft.EntityFrameworkCore.SqlServer -ContextDir "C:\MySolution\DataAccessLayer\Context" -OutputDir "C:\MySolution\DomainLayer\Models" -Namespace "DomainLayer.Models" -ContextNamespace "DataAccessLayer.Context"
Scaffold-DbContext to different output folder:
Scaffold-DbContext 'ConnectionString' Microsoft.EntityFrameworkCore.SqlServer -ContextDir MyDbContextFolder -OutputDir EntitiesFolder
dotnet ef dbcontext scaffold 'ConnectionString' Microsoft.EntityFrameworkCore.SqlServer --context-dir MyDbContextFolder --output-dir EntitiesFolder
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
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?
As indicated by ihebiheb Here is an example
context-namespace, namespace for your class context-dir, folder in which you will leave the context of your classes namespace, space name for each class in the database output-dir, output folder for each class in the database f, to overwrite the data that exist in the output folder for each class in the database
dotnet ef dbcontext scaffold "connectionstring" Microsoft.EntityFrameworkCore.SqlServer --context-namespace "apitienda.dal.Context" --context-dir "Context" --namespace "apitienda.dto.Entities" --output-dir "../apitienda.dto/Entities" -f
tienda --apitienda.api --apitienda.dto --Entities --apitienda.dal --Context
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.