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

New overload: implementing IModelCacheKeyFactory #3305

Merged
merged 8 commits into from
Jun 28, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions entity-framework/core/modeling/dynamic-model.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ The following implementation takes the `UseIntProperty` into account when produc

[!code-csharp[Main](../../../samples/core/Modeling/DynamicModel/DynamicModelCacheKeyFactory.cs?name=DynamicModel)]

If you have to support [Design-time DbContext creation](xref:core/cli/dbcontext-creation)(Needed by, for example, the Migration commands), you have to use the new overload for the Create method. As in the following example:
joelmandell marked this conversation as resolved.
Show resolved Hide resolved

[!code-csharp[Main](../../../samples/core/Modeling/DynamicModel/DynamicModelCacheKeyFactoryDesignTimeSupport.cs?name=DynamicModelDesignTimeSupport)]

Finally, register your new `IModelCacheKeyFactory` in your context's `OnConfiguring`:

[!code-csharp[Main](../../../samples/core/Modeling/DynamicModel/DynamicContext.cs?name=OnConfiguring)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;

namespace EFModeling.DynamicModel
{
#region DynamicModelDesignTimeSupport
public class DynamicModelCacheKeyFactory : IModelCacheKeyFactory
joelmandell marked this conversation as resolved.
Show resolved Hide resolved
{
public object Create(DbContext context, bool designTime)
=> context is DynamicContext dynamicContext
? (context.GetType(), dynamicContext.UseIntProperty, designTime)
: (object)context.GetType();

public object Create(DbContext context)
=> Create(context, false);
}
#endregion
}