Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuring an IModelCacheKeyFactory causes "the requested configuration is not stored in the read-optimized model" #3851

Closed
roji opened this issue Apr 28, 2022 · 6 comments · Fixed by #4050

Comments

@roji
Copy link
Member

roji commented Apr 28, 2022

With 6.0.4, configuring an IModelCacheKeyFactory causes the following to be immediately thrown:

Unhandled exception. System.InvalidOperationException: The requested configuration is not stored in the read-optimized model, please use 'DbContext.GetService<IDesignTimeModel>().Model'.
   at Microsoft.EntityFrameworkCore.RelationalModelExtensions.GetCollation(IReadOnlyModel model)
   at Microsoft.EntityFrameworkCore.Metadata.IRelationalModel.get_Collation()
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.CreateCreateOperations()
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerDatabaseCreator.CreateAsync(CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreatedAsync(CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreatedAsync(CancellationToken cancellationToken)
   at Program.<Main>$(String[] args) in /home/roji/projects/test/Program.cs:line 12
   at Program.<Main>$(String[] args) in /home/roji/projects/test/Program.cs:line 12
   at Program.<Main>(String[] args)

This does not happen in 5.0. If this is intentional for some reason, we may be missing a breaking change note?

/cc @AndriySvyryd

Repro
await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();

public class BlogContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        => optionsBuilder
            .UseSqlServer(@"Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
            .ReplaceService<IModelCacheKeyFactory, MyModelCacheKeyFactory>();
}

public class MyModelCacheKeyFactory : IModelCacheKeyFactory
{
    public object Create(DbContext context)
        =>  context.GetType();
}

public class Blog
{
    public int Id { get; set; }
    public string Name { get; set; }
}
@ajcvickers
Copy link
Member

Was docced as part of dotnet/efcore#25154, but no sure if it is noted as a breaking changed.

@roji
Copy link
Member Author

roji commented Apr 28, 2022

Thanks, missed that. Leaving open to see about adding a breaking change note.

@ajcvickers ajcvickers transferred this issue from dotnet/efcore May 7, 2022
@ajcvickers ajcvickers added this to the 7.0.0 milestone May 7, 2022
@sjb-sjb
Copy link

sjb-sjb commented May 9, 2022

I got the same error when I created a DbContext and tried to invoke dbContext.Model.GetChangeTrackingStrategy().
I have not configured any IModelCacheKeyFactory. Using dbContext.GetService().Model as recommended in the exception seemed to get around that exception but (a) I then got the exception when calling dbContext.Model.GetPropertyAccessMode(), and also (b) it requires using the Infrastructure namespace, which doesn't feel right for a user app.

@AndriySvyryd
Copy link
Member

@sjb-sjb Why do you need to call dbContext.Model.GetChangeTrackingStrategy() and dbContext.Model.GetPropertyAccessMode()?

@sjb-sjb
Copy link

sjb-sjb commented May 13, 2022

Well, I want to check the caller’s configuration of the dbcontext to make sure it matches what my library requires.

@ajcvickers ajcvickers modified the milestones: 7.0.0, 6.0.0 Sep 20, 2022
@ajcvickers
Copy link
Member

Poaching

ajcvickers added a commit that referenced this issue Sep 21, 2022
ajcvickers added a commit that referenced this issue Sep 21, 2022
ajcvickers added a commit that referenced this issue Sep 21, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants