question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scaffolding using separate assemblies for model/context

See original GitHub issue

Scaffoling 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:closed
  • Created 7 years ago
  • Comments:14 (2 by maintainers)

github_iconTop GitHub Comments

31reactions
peterrescommented, Oct 16, 2017

@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; }

16reactions
Pavel-Malezhykcommented, Aug 25, 2017

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; }

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found