dotnet / dotnet/Scaffolding

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

Open
#3,771 0 comments 1 reaction 2 assignees Claimed by @haileymck View on GitHub
area-dotnet-scaffolding customer-reported
Dominant language
C#
Stars
818
Forks
260
Avg merge
1d 8h
Merged PRs (30d)
10

Description

. 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

```







all
runtime; build; native; contentfiles; analyzers; buildtransitive



all
runtime; build; native; contentfiles; analyzers; buildtransitive





```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.