Skip to content

Scaffolding doesn't work when DbContext is in a separate project #3771

@orange2-netizen

Description

@orange2-netizen

. My DataContext and Models are in a separated project and I am using Project Reference. The error only happens when the DataContext and Models are in a separated project. I tested a MVC project with DataContext and Models within the same project, the scaffolding works fine.

my Visual Studio version is 18.6.2 and i am using dotnet 10.0.300. It used to be working fine (i do not remember what updates I have installed)

below is the output.

C:\Program Files\dotnet\dotnet.exe scaffold aspnet mvccontroller-crud --model MyModel --controller MyModelsController --dataContext MyDbContext --dbProvider sqlserver-efcore --views --project "C:\MyProject\MyProject.csproj"
Initializing scaffolding model...
Invalid  --model  'MyModel'
An error occurred.

using the older version command line worked.

PS C:\MyProject> dotnet aspnet-codegenerator controller -name MyModelsController -m MyLibrary.Core.Models.MyModel -dc MyLibrary.Core.Data.MyDbContext  --relativeFolderPath Controllers --useDefaultLayout --referenceScriptLibraries --readWriteActions
Building project ...
Finding the generator 'controller'...
Running the generator 'controller'...

Minimal hosting scenario!
Attempting to figure out the EntityFramework metadata for the model and DbContext: 'MyModel'

Using database provider 'Microsoft.EntityFrameworkCore.SqlServer'!

Added Controller : '\Controllers\MyModelsController.cs'.
Added View : \Views\MyModels\Create.cshtml
Added View : \Views\MyModels\Edit.cshtml
Added View : \Views\MyModels\Details.cshtml
Added View : \Views\MyModels\Delete.cshtml
Added View : \Views\MyModels\Index.cshtml
RunTime 00:00:24.39

using the new one does NOT work even if I use fully-qualified type names

C:\Program Files\dotnet\dotnet.exe scaffold aspnet mvccontroller-crud --model MyLibrary.Core.Models.MyModel --controller MyModelsController --dataContext MyLibrary.Core.Data.MyDbContext --dbProvider sqlserver-efcore --views --project "C:\MyProject\MyProject.csproj"

Invalid --dataContext option 
Using default 'NewDbContext' 
Initializing scaffolding model... 
Invalid --model 'MyLibrary.Core.Models.MyModel' 
An error occurred.

this is my package reference in the mvc project

<PackageReference Include="Azure.Core" Version="1.57.0" />
    <PackageReference Include="Azure.Identity" Version="1.21.0" />
    <PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="10.0.8" />
    <PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="10.0.8" />
    <PackageReference Include="Microsoft.Bcl.Cryptography" Version="10.0.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.8">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="10.0.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="10.0.8">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.IdentityModel.Abstractions" Version="8.18.0" />
    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="10.0.2" />
    <PackageReference Include="NuGet.Packaging" Version="7.6.0" />
    <PackageReference Include="NuGet.Protocol" Version="7.6.0" />

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions