AddDbContext: Violates the constraint of type parameter 'TContext'
See original GitHub issueHi, I have an ASP.NET Core 2.2 Web API app running fine in the browser. When I try to generate Typescript files in NSwagStudio, I get the following errors:
System.Security.VerificationException:
Method Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext:
type argument 'MovieWatcher.Server.Models.MovieTrackerContext' violates the constraint of type parameter 'TContext'.
Runtime: NetCore22
at MovieWatcher.Server.Startup.ConfigureServices(IServiceCollection services)
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Hosting.ConventionBasedStartup.ConfigureServices(IServiceCollection services)
at Microsoft.AspNetCore.Hosting.Internal.WebHost.EnsureApplicationServices()
at Microsoft.AspNetCore.Hosting.Internal.WebHost.Initialize()
at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
at NSwag.Commands.Generation.OpenApiGeneratorCommandBase`1.CreateWebHostAsync(AssemblyLoader assemblyLoader) in C:\projects\nswag\src\NSwag.Commands\Commands\Generation\OpenApiGeneratorCommandBase.cs:line 318
at NSwag.Commands.Generation.AspNetCore.AspNetCoreToSwaggerCommand.RunIsolatedAsync(AssemblyLoader assemblyLoader) in C:\projects\nswag\src\NSwag.Commands\Commands\Generation\AspNetCore\AspNetCoreToOpenApiCommand.cs:line 309
at NSwag.Commands.IsolatedCommandBase`1.IsolatedCommandAssemblyLoader`1.Run(String commandType, String commandData, String[] assemblyPaths, String[] referencePaths) in C:\projects\nswag\src\NSwag.Commands\Commands\IsolatedCommandBase.cs:line 71
at NSwag.Commands.IsolatedCommandBase`1.<>c__DisplayClass17_0.<RunIsolatedAsync>b__0() in C:\projects\nswag\src\NSwag.Commands\Commands\IsolatedCommandBase.cs:line 61
at System.Threading.Tasks.Task`1.InnerInvoke()
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location where exception was thrown ---
at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot)
--- End of stack trace from previous location where exception was thrown ---
at NSwag.Commands.IsolatedCommandBase`1.RunIsolatedAsync(String configurationFile)
at NSwag.Commands.IsolatedSwaggerOutputCommandBase`1.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\IsolatedSwaggerOutputCommandBase.cs:line 47
at NSwag.Commands.Generation.AspNetCore.AspNetCoreToSwaggerCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\Generation\AspNetCore\AspNetCoreToOpenApiCommand.cs:line 94
at NSwag.Commands.NSwagDocumentBase.GenerateSwaggerDocumentAsync() in C:\projects\nswag\src\NSwag.Commands\NSwagDocumentBase.cs:line 279
at NSwag.Commands.NSwagDocument.ExecuteAsync() in C:\projects\nswag\src\NSwag.Commands\NSwagDocument.cs:line 81
at NSwag.Commands.Document.ExecuteDocumentCommand.ExecuteDocumentAsync(IConsoleHost host, String filePath) in C:\projects\nswag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommand.cs:line 85
at NSwag.Commands.Document.ExecuteDocumentCommand.RunAsync(CommandLineProcessor processor, IConsoleHost host) in C:\projects\nswag\src\NSwag.Commands\Commands\Document\ExecuteDocumentCommand.cs:line 32
at NConsole.CommandLineProcessor.ProcessSingleAsync(String[] args, Object input)
at NConsole.CommandLineProcessor.ProcessAsync(String[] args, Object input)
at NConsole.CommandLineProcessor.Process(String[] args, Object input)
at NSwag.Commands.NSwagCommandProcessor.Process(String[] args) in C:\projects\nswag\src\NSwag.Commands\NSwagCommandProcessor.cs:line 56
Here’s the DbContext I have:
public class MovieTrackerContext : IdentityDbContext<ApplicationUser>
{
public MovieTrackerContext(DbContextOptions<MovieTrackerContext> options)
: base(options)
{ }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Movie>(entity =>
{
entity.Property(e => e.Id).ValueGeneratedOnAdd();
entity.Property(e => e.Title).IsRequired();
entity.Property(e => e.WatchedOn).IsRequired().HasColumnType("date");
entity.Property(e => e.Rating).HasDefaultValue(0);
});
modelBuilder.Entity<Movie>().HasData(
new Movie() { Id = 1, Title = "The Shawshank Redemption", WatchedOn = new DateTime(2016, 11, 4), Genre = "Drama", Rating = 4 },
new Movie() { Id = 2, Title = "The Godfather", WatchedOn = new DateTime(2017, 10, 2), Genre = "Drama", Rating = 2 },
new Movie() { Id = 3, Title = "The Dark Knight", WatchedOn = new DateTime(2018, 12, 1), Genre = "Drama, Action", Rating = 3 },
new Movie() { Id = 4, Title = "The Godfather: Part II ", WatchedOn = new DateTime(2019, 2, 4), Genre = "Drama", Rating = 1 },
new Movie() { Id = 5, Title = "The Lord of the Rings: The Return of the King", WatchedOn = new DateTime(2019, 4, 2), Genre = "Adventure, Drama, Fantasy", Rating = 5 },
new Movie() { Id = 6, Title = "Pulp Fiction", WatchedOn = new DateTime(2019, 3, 27), Genre = "Crime, Drama", Rating = 3 });
base.OnModelCreating(modelBuilder);
}
public DbSet<Movie> Movies { get; set; }
}
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top Results From Across the Web
IDesignTimeDbContextFactory[TContext] violates the
EntityFrameworkCore.Design.IDesignTimeDbContextFactory 1[TContext]' violates the constraint of type parameter 'TContext'. at System.
Read more >DbContext violates the constraint of type 'TContext'
SiteContext' violates the constraint of type parameter 'TContext'. This error came up when I was working with a DBContext in a separate project ......
Read more >C# – IDesignTimeDbContextFactory[TContext] violates the
EntityFrameworkCore.Design.IDesignTimeDbContextFactory 1[TContext]' violates the constraint of type parameter 'TContext'. at System.RuntimeTypeHandle.
Read more >Design-time DbContext Creation - EF Core
Using a constructor with no parameters. If the DbContext can't be obtained from the application service provider, the tools look for the derived ......
Read more >ASP Core violates the constraint of type parameter 'TRole' for ...
Coding example for the question ASP Core violates the constraint of type parameter 'TRole' for String-entityframework core.
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 Free
Top 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
When generating via CLI it might not find the appsettings.json and some services might not register correctly. An option is to check whether it’s running in the nswag context and not register unnecessary services or use an nswag only startup.cs (a second one), just catch the exception and use that to find out in which context it’s running, etc.
I had this same issue when trying to use the LazyCache library - see https://github.com/alastairtree/LazyCache/issues/186. In that case, it turned out to be because LazyCache expects to be the first library to register an
IMemoryCache
singleton in its recommendedservices.AddLazyCache()
call in Startup.cs, whereas the NSwag generation process injects its ownIMemoryCache
into the DI container before my Startup.cs is ever called. The workaround in my case was to ensure thatLazyCache
gets its ownIMemoryCache
instance rather than using the default DI-based setup process: https://github.com/CareTogether/CareTogetherCMS/commit/076200d81c4c8ca868fd9f83c2d8122fd6339ff4