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.

`Optimize-DbContext` Does Not Fully Qualify Types, Resulting in Compile Errors

See original GitHub issue

File a bug

When using Optimize-DbContext, the generated types are not fully qualified and/or do not fully resolve the generated type, resulting in compile errors.

I have identified 3 scenarios:

  1. The model type extends a class which has the same name of the class it extends (e.g. IdentityUser )
  2. The model type uses the same name as a type found in the System namespace (e.g. Index)
  3. The model type has the same name as the namespace in which it is located (e.g. Definition.Definition)

Include your code

I have committed a SLN with the generated compile errors here for your review: https://github.com/Mike-E-angelo/Stash/blob/master/EfCore6.Model/EfCore6.Model.sln

Include stack traces

NA

Include verbose output

PM> Optimize-DbContext -OutputDir Optimized -Verbose
Using project 'EfCore6.Model'.
Using startup project 'EfCore6.Model'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile ...\Mike-E-angelo\Stash\EfCore6.Model\EfCore6.Model\bin\Debug\net6.0\EfCore6.Model.deps.json --additionalprobingpath C:\Users\micha\.nuget\packages --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig ...\Mike-E-angelo\Stash\EfCore6.Model\EfCore6.Model\bin\Debug\net6.0\EfCore6.Model.runtimeconfig.json C:\Users\micha\.nuget\packages\microsoft.entityframeworkcore.tools\6.0.0-preview.7.21378.4\tools\netcoreapp2.0\any\ef.dll dbcontext optimize --output-dir Optimized --verbose --no-color --prefix-output --assembly ...\Mike-E-angelo\Stash\EfCore6.Model\EfCore6.Model\bin\Debug\net6.0\EfCore6.Model.dll --project ...\Mike-E-angelo\Stash\EfCore6.Model\EfCore6.Model\EfCore6.Model.csproj --startup-assembly ...\Mike-E-angelo\Stash\EfCore6.Model\EfCore6.Model\bin\Debug\net6.0\EfCore6.Model.dll --startup-project ...\Mike-E-angelo\Stash\EfCore6.Model\EfCore6.Model\EfCore6.Model.csproj --project-dir ...\Mike-E-angelo\Stash\EfCore6.Model\EfCore6.Model\ --language C# --working-dir ...\Mike-E-angelo\Stash\EfCore6.Model --root-namespace EfCore6.Model --nullable
Using assembly 'EfCore6.Model'.
Using startup assembly 'EfCore6.Model'.
Using application base '...\Mike-E-angelo\Stash\EfCore6.Model\EfCore6.Model\bin\Debug\net6.0'.
Using working directory '...\Mike-E-angelo\Stash\EfCore6.Model\EfCore6.Model'.
Using root namespace 'EfCore6.Model'.
Using project directory '...\Mike-E-angelo\Stash\EfCore6.Model\EfCore6.Model\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Found IDesignTimeDbContextFactory implementation 'StorageBuilder'.
Found DbContext 'ApplicationState'.
Finding application service provider in assembly 'EfCore6.Model'...
Finding Microsoft.Extensions.Hosting service provider...
No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
No application service provider was found.
Finding DbContext classes in the project...
Using DbContext factory 'StorageBuilder'.
Using context 'ApplicationState'.
Finding design-time services referenced by assembly 'EfCore6.Model'...
Finding design-time services referenced by assembly 'EfCore6.Model'...
No referenced design-time services were found.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding IDesignTimeServices implementations in assembly 'EfCore6.Model'...
No design-time services were found.
The index {'UserId'} was not created on entity type 'IdentityUserRole<int>' as the properties are already covered by the index {'UserId', 'RoleId'}.
The index {'UserId'} was not created on entity type 'IdentityUserToken<int>' as the properties are already covered by the index {'UserId', 'LoginProvider', 'Name'}.
The property 'Index.UserId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'User.DefinitionId' was created in shadow state because there are no eligible CLR members with a matching name.
Successfully generated a compiled model, to use it call 'options.UseModel(ApplicationStateModel.Instance)'. Run this command again when the model is modified.
'ApplicationState' disposed.

Include provider and version information

EF Core version: 6.0.0-preview.7.21378.4 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: net6.0 Operating system: Windows 10 IDE: Visual Studio 2022 Preview 3.0

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:28 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
AndriySvyrydcommented, Aug 12, 2022

This tracks adding a simple commandline switch - https://github.com/dotnet/efcore/issues/27203

1reaction
AndriySvyrydcommented, Aug 20, 2021

BTW about https://devblogs.microsoft.com/dotnet/announcing-entity-framework-core-6-0-preview-5-compiled-models/#comment-10060

I couldn’t answer because comments are now closed. You can use this to get your model “size”:

var model = context.Model;

Console.WriteLine("Model has:");
Console.WriteLine($"  {model.GetEntityTypes().Count()} entity types");
Console.WriteLine($"  {model.GetEntityTypes().SelectMany(e => e.GetDeclaredProperties()).Count()} properties");
Console.WriteLine($"  {model.GetEntityTypes().SelectMany(e => e.GetDeclaredForeignKeys()).Count()} relationships");

But the benefit you get from a compiled model also depends on the shape of your model. In general if the startup time wasn’t bothering you that much previously you probably won’t notice a difference with a compiled model.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add a parameter to Optimize-DbContext for fully qualifying ...
Optimize -DbContext Does Not Fully Qualify Types, Resulting in Compile Errors ... Running with compiled model fails to load entity properties ...
Read more >
Unable to resolve service for type ¨Microsoft. ...
I fixed this error by specifying --project and --startup-project options to EF Core CLI tools like this: dotnet ef database update --verbose ...
Read more >
EF Core tools reference (.NET CLI)
The DbContext class to use. Class name only or fully qualified with namespaces. If this option is omitted, EF Core will find the...
Read more >
EF Core tools reference (Package Manager Console)
The DbContext class to use. Class name only or fully qualified with namespaces. If this parameter is omitted, EF Core finds the context...
Read more >
Type-name syntax - Progress Documentation
All qualified type names must be fully qualified, using a complete package-name . ABL does not support partially qualified type names using a...
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