Scaffolding using separate assemblies for model/context
See original GitHub issueScaffoling using dotnet aspnet-codgenerator –commands only seems to work if the model and DbContext are located in the same assembly as the Mvc app.
Steps to reproduce
1. Add domain model to it's own assembly, i.e. Blog class in AssemblyName.Domain
2. Add EF Context to its own assembly with DbSet<Blog>, i.e. ModelContext class in Assembly.Data
3. Create MvcApp referencing both assemblies above and load ModelContext into DI.
4. Run :: dotnet aspnet-codegenerator --project . controller -name BlogsController -m Blog -dc ModelContext -outDir Controllers
Expected behavior
Writes BlogsController class to the Controllers folder using supplied model and DbContext.
Actual behavior
Could not add Model type 'AssemblyName.Domain.Blog' to DbContext 'AssemblyName.Data.ModelContext'. Please make sure that 'AssemblyName.Data.ModelContext' has a DbS
et property for 'AssemblyName.Domain.Blog'.
at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.<BuildCommandLine>b__6_0()
at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)
Environment
dotnet --info output:
.NET Command Line Tools (1.0.0-rc4-004771)
Product Information:
Version: 1.0.0-rc4-004771
Commit SHA-1 hash: d881d45b75
Runtime Environment:
OS Name: Windows
OS Version: 10.0.14393
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\1.0.0-rc4-004771
.csproj file
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp1.1</TargetFramework>
<PackageTargetFallback>portable-net45;win8;wp8;wpa81</PackageTargetFallback>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.1" />
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="1.1.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer.Design" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="1.1.0" />
<ProjectReference Include="..\AssemblyName.Data\AssemblyName.Data.csproj" />
<ProjectReference Include="..\AssemblyName.Domain\AssemblyName.Domain.csproj" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="1.0.0-msbuild3-final" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="1.0.0-msbuild3-final" />
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0-msbuild3-final" />
</ItemGroup>
</Project>
Issue Analytics
- State:
- Created 7 years ago
- Comments:14 (2 by maintainers)
Top Results From Across the Web
Scaffolding using separate assemblies for model/context
Scaffoling using dotnet aspnet-codgenerator --commands only seems to work if the model and DbContext are located in the same assembly as the Mvc ......
Read more >MVC4 Scaffolding, DBContext in different assembly
I've been trying to scaffold out a DBContext class (Code first), which resides in a different assembly with the reference added to the ......
Read more >Custom Reverse Engineering Templates - EF Core
Using T4 text templates to customize the scaffolded code when reverse engineering an Entity Framework Core model from a database.
Read more >Migrations with Multiple Providers - EF Core
In this article. Using multiple context types; Using one context type. The EF Core Tools only scaffold migrations for the active provider.
Read more >EF Core 6 Database First / DB First (Entity ... - YouTube
NET Core Web API Project 04:50 Install NuGet Packages 06:04 Scaffold the DbContext 08:44 Check the created Models 10:37 Register the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@Pavel-Malezhyk thanks, same situation for me, netcoreapp2.0 with separate projects, and you were right, would never thought of this.
But it’s really weird, using full type qualifier with DbSet<T> in DbContext solves the problem.
Not working:
public DbSet<DataClass> DataClasses { get; set; }
Working:
public DbSet<AppDomain.DataClass> DataClasses { get; set; }
I had the same problem: (netcoreapp2.0, EF core) I had such projects and references: WebProj (need to add Controller)->AppData (DbContext)->AppDomain(Data class) … scaffolding had crushed with “Could not add Model type” error … after I added Explicit link to AppDomain despite of using “use directive” and all started to work well. public DbSet<DataClass> DataClasses { get; set; } -> public DbSet<AppDomain.DataClass> DataClasses { get; set; }