Search code examples
c#entity-frameworkasp.net-coreef-code-firstvisual-studio-2017

how to scaffold-dbcontext to different folders?


We can run the following command that will create the context and models in the project:

Scaffold-DbContext -Connection "Data Source=somedatasource.com;Initial Catalog=mydb;Integrated Security=False;User Id=user;Password=pass;MultipleActiveResultSets=True" -Provider Microsoft.EntityFrameworkCore.SqlServer -Tables PrdSalesRegion -Context SalesRegionContext

How do we specify different output folders for context and tables?

When I attempt to do this:

Scaffold-DbContext -Connection "Data Source=somedatasource.com;Initial Catalog=mydb;Integrated Security=False;User Id=user;Password=pass;MultipleActiveResultSets=True" -Provider Microsoft.EntityFrameworkCore.SqlServer -Tables Models/PrdSalesRegion -Context Context/SalesRegionContext

Note, that the only change is:

-Tables Models/PrdSalesRegion -Context Context/SalesRegionContext

Then I get this output:

The context class name passed in, Context/SalesRegionContext, is not a valid C# identifier.

How do we specify different output folders for context and tables?


Solution

  • You can add the flag -o <DIR>. You could also use -f flag to override existing files while updating existing models with new fields.

    The full command could be:

    Scaffold-DbContext "Host=127.0.0.1;Database=dbname;Username=postgres;Password=test12345"Npgsql.EntityFrameworkCore.PostgreSQL -o Entities -f
    

    Hope this helps