c# - Entity Framework Core - 禁用模型缓存,为每个实例调用 onModelCreating() dcontext
问题描述
文档说:该上下文的模型已缓存,并且适用于应用程序域中上下文的所有进一步实例。可以通过在给定的 ModelBuidler 上设置 ModelCaching 属性来禁用此缓存
但我找不到办法。我必须禁用缓存,因为我在运行时添加模型并从程序集中加载所有模型并创建数据库。
我发现这个链接说实现这一点的一种方法是使用 DBModelBuilding - 手动将模型添加到上下文,但它适用于实体框架,对 EF Core 没有帮助。
我希望有人对此有解决方案。
谢谢
解决方案
成功创建模型后,EF Core 将永久缓存它,除非您实现一个能够判断一个模型是否与另一个模型等效的缓存管理器,因此它可以被缓存或不被缓存。
入口点是实现缓存管理器:
internal sealed class MyModelCacheKeyFactory : IModelCacheKeyFactory
{
public object Create([NotNull] DbContext context)
{
return GetKey(context);
}
}
您必须编写的GetKey
方法必须返回一个将用作键的对象。此方法应检查提供的上下文并在模型相同时返回相同的键,而在模型不同时返回不同的键。更多关于IModelCacheKeyFactory 接口。
我明白,这可能不清楚(而且也不适合我),所以我写了一个完整的例子来说明我在生产中所拥有的东西。
一个工作示例
我的目标是为不同的模式使用相同的上下文。我们需要做的是
- 创建一个新的上下文选项
- 在上下文中实现逻辑
- 创建缓存密钥工厂
- 使扩展方法指定架构
- 在 db 上下文中调用扩展方法
1.创建一个新的上下文选项
这里有一个样板_schemaName
仅包含。样板是必要的,因为扩展选项在设计上是不可变的,我们需要保留合同。
internal class MySchemaOptionsExtension : IDbContextOptionsExtension
{
private DbContextOptionsExtensionInfo? _info;
private string _schemaName = string.Empty;
public MySchemaOptionsExtension()
{
}
protected MySchemaOptionsExtension(MySchemaOptionsExtension copyFrom)
{
_schemaName = copyFrom._schemaName;
}
public virtual DbContextOptionsExtensionInfo Info => _info ??= new ExtensionInfo(this);
public virtual string SchemaName => _schemaName;
public virtual void ApplyServices(IServiceCollection services)
{
// not used
}
public virtual void Validate(IDbContextOptions options)
{
// always ok
}
public virtual MySchemaOptionsExtension WithSchemaName(string schemaName)
{
var clone = Clone();
clone._schemaName = schemaName;
return clone;
}
protected virtual MySchemaOptionsExtension Clone() => new(this);
private sealed class ExtensionInfo : DbContextOptionsExtensionInfo
{
private const long ExtensionHashCode = 741; // this value has chosen has nobody else is using it
private string? _logFragment;
public ExtensionInfo(IDbContextOptionsExtension extension) : base(extension)
{
}
private new MySchemaOptionsExtension Extension => (MySchemaOptionsExtension)base.Extension;
public override bool IsDatabaseProvider => false;
public override string LogFragment => _logFragment ??= $"using schema {Extension.SchemaName}";
public override long GetServiceProviderHashCode() => ExtensionHashCode;
public override void PopulateDebugInfo([NotNull] IDictionary<string, string> debugInfo)
{
debugInfo["MySchema:" + nameof(DbContextOptionsBuilderExtensions.UseMySchema)] = (ExtensionHashCode).ToString(CultureInfo.InvariantCulture);
}
}
}
2.上下文中的逻辑
在这里,我们将模式强制用于所有真实实体。模式是通过附加到上下文的选项获得的
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
var options = this.GetService<IDbContextOptions>().FindExtension<MySchemaOptionsExtension>();
if (options == null)
{
// nothing to apply, this is a supported scenario.
return;
}
var schema = options.SchemaName;
foreach (var item in modelBuilder.Model.GetEntityTypes())
{
if (item.ClrType != null)
item.SetSchema(schema);
}
}
3.创建缓存密钥工厂
在这里,我们需要创建缓存工厂,它将告诉 EF Core 它可以缓存同一上下文中的所有模型,即具有相同架构的所有上下文将使用相同的模型:
internal sealed class MyModelCacheKeyFactory : IModelCacheKeyFactory
{
public object Create([NotNull] DbContext context)
{
const string defaultSchema = "dbo";
var extension = context.GetService<IDbContextOptions>().FindExtension<MySchemaOptionsExtension>();
string schema;
if (extension == null)
schema = defaultSchema;
else
schema = extension.SchemaName;
if (string.IsNullOrWhiteSpace(schema))
schema = defaultSchema;
// ** this is the magic **
return (context.GetType(), schema.ToUpperInvariant());
}
}
魔法就在这条线上
return (context.GetType(), schema.ToUpperInvariant());
我们返回一个包含上下文类型和模式的元组。元组的哈希结合了每个条目的哈希,因此类型和模式名称是这里的逻辑鉴别符。当它们匹配时,模型被重用;如果他们不这样做,则会创建一个新模型,然后进行缓存。
4.制作扩展方法
扩展方法只是隐藏了选项的添加和缓存服务的替换。
public static DbContextOptionsBuilder UseMySchema(this DbContextOptionsBuilder optionsBuilder, string schemaName)
{
if (optionsBuilder == null)
throw new ArgumentNullException(nameof(optionsBuilder));
if (string.IsNullOrEmpty(schemaName))
throw new ArgumentNullException(nameof(schemaName));
var extension = optionsBuilder.Options.FindExtension<MySchemaOptionsExtension>() ?? new MySchemaOptionsExtension();
extension = extension.WithSchemaName(schemaName);
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);
optionsBuilder.ReplaceService<IModelCacheKeyFactory, MyModelCacheKeyFactory>();
return optionsBuilder;
}
特别是,以下行应用了我们的缓存管理器:
optionsBuilder.ReplaceService<IModelCacheKeyFactory, MyModelCacheKeyFactory>();
5.调用扩展方法
您可以手动创建上下文,如下所示:
var options = new DbContextOptionsBuilder<DataContext>();
options.UseMySchema("schema1")
options.UseSqlServer("connection string omitted");
var context = new DataContext(options.Options)
或者,您可以使用IDbContextFactory
依赖注入。更多关于IDbContextFactory 接口。
推荐阅读
- javascript - jQuery循环遍历多维数组并显示每个父数组的子数组
- python - 当 python 脚本存储在树莓派上,而不是在我有我的 php 脚本的同一台计算机上时,在启动时从 php 运行 Python 脚本?
- python - 如何将链式规则应用于多个 tf.GradientTape?
- ssh - 等待屏幕 -X stuff "script" 完成
- kubernetes - 为什么使用 Job API 时 Kubernetes 成功计数会下降
- postman - 邮递员标签组ipng
- shopware - 在生产环境中找不到控制器路由
- python-3.x - 二叉树的遍历:我无法在 python 中得到正确的输出
- oracle - 转换为时间戳
- javascript - 如何检测IOS是否已经安装了PWA