efcore: Error running EnsureCreated(). What am I doing wrong?
Hello! I tried to use EnsureCreated in my Startup.cs, Configure()-method.
using var scope = app.ApplicationServices.CreateScope();
using var context = scope.ServiceProvider.GetService<WPContext>();
context.Database.EnsureDeleted();
context.Database.EnsureCreated();
Throws this exception:
System.InvalidOperationException: 'The requested configuration is not stored in the read-optimized model, please use 'DbContext.DesignTimeModel'.'
Stack trace
at Microsoft.EntityFrameworkCore.Metadata.SlimEntityType.Microsoft.EntityFrameworkCore.Metadata.IReadOnlyEntityType.GetSeedData(Boolean providerValues)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.TrackData(IRelationalModel source, IRelationalModel target, DiffContext diffContext)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.<GetDataOperations>d__74.MoveNext()
at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IRelationalModel source, IRelationalModel target)
at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.GetCreateTablesCommands(MigrationsSqlGenerationOptions options)
at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.CreateTables()
at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreated()
at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated()
at WorkPartnerLib.Startup.Configure(IApplicationBuilder app, IWebHostEnvironment env, IHubContext`1 baseHub, IDistributedCache cache, IHttpContextAccessor httpContextAccessor) in C:\Users\utvec\Programmering\WorkPartnerLib\WorkPartnerLib\Startup.cs:line 187
at System.RuntimeMethodHandle.InvokeMethod(Object target, Span`1& arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.Invoke(Object instance, IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.ConfigureBuilder.<>c__DisplayClass4_0.<Build>b__0(IApplicationBuilder builder)
at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass15_0.<UseStartup>b__1(IApplicationBuilder app)
at Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderStartupFilter.<>c__DisplayClass0_0.<Configure>g__MiddlewareFilterBuilder|0(IApplicationBuilder builder)
at Microsoft.AspNetCore.Server.IIS.Core.IISServerSetupFilter.<>c__DisplayClass2_0.<Configure>b__0(IApplicationBuilder app)
at Microsoft.AspNetCore.HostFilteringStartupFilter.<>c__DisplayClass0_0.<Configure>b__0(IApplicationBuilder app)
at Microsoft.AspNetCore.Hosting.GenericWebHostService.<StartAsync>d__34.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
at Microsoft.Extensions.Hosting.Internal.Host.<StartAsync>d__11.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.ConfiguredTaskAwaitable.ConfiguredTaskAwaiter.GetResult()
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.<RunAsync>d__4.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
at Microsoft.Extensions.Hosting.HostingAbstractionsHostExtensions.Run(IHost host)
at WorkPartnerLib.Program.Main(String[] args) in C:\Users\utvec\Programmering\WorkPartnerLib\WorkPartnerLib\Program.cs:line 17
Version
EF Core version: 6.0 preview4 (tried with preview5 also). Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 6.0 Operating system: Windows 10 IDE: Visual Studio 2022 Preview
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 17 (6 by maintainers)
Links to this issue
- EF Core 6.0 中的中断性变更 - EF Core | Microsoft Learn
- Критические изменения в EF Core 6.0 — EF Core | Microsoft Learn
- Memutus perubahan dalam EF Core 6.0 - EF Core | Microsoft Learn
- EF Core 6.0'da hataya neden olan değişiklikler - EF Core | Microsoft Learn
- Alterações interruptivas no EF Core 6.0 – EF Core | Microsoft Learn
- Istotne zmiany w programie EF Core 6.0 — EF Core | Microsoft Learn
- Changements cassants dans EF Core 6.0 - EF Core | Microsoft Learn
- Breaking changes in EF Core 6.0 - EF Core | Microsoft Learn
- Modifiche di rilievo in EF Core 6.0 - EF Core | Microsoft Learn
- EF Core 6.0 での破壊的変更 - EF Core | Microsoft Learn
- Zásadní změny v EF Core 6.0 – EF Core | Microsoft Learn
- EF Core 6.0의 호환성이 손상되는 변경 - EF Core | Microsoft Learn
- Cambios importantes en EF Core 6.0: EF Core | Microsoft Learn
- EF Core: Breaking Changes in EF Core 6.0 | Microsoft Learn
- EF Core 6.0 的重大變更 - EF Core | Microsoft Learn
Should we add an example to the docs? (If it’s not there already.)
If you have a custom
IModelCacheKeyFactory
implementation make sure to implement the new overload.@lucianbrezaiu The easiest is a tuple
(context.GetType(), designTime)
. But otherwise any type that uses the values ofcontext.GetType()
(if you use more than one DbContext type in the application) anddesignTime
inEquals
andGetHashCode
should work.As @hounddog22030 showed a string value can also be constructed, but this is less recommended as it causes additional allocations.
I am experiencing this problem. I have reviewed the “New IModelCacheKeyFactory implementation”, but frankly I don’t understand what it is doing or why…
Our exception is:
We are using the in memory database via
Our
IModelCacheKeyFactory
is as follows:}
Everything is updated to latest (6.0.2)
What should I be looking @?