Cra*_*azy 5 c# entity-framework
在我们的应用程序中有大量的表(大约 50k)——所有这些表都被实际使用,这导致实体框架中的内存消耗很高。
在进行了一些内存分析后,我注意到 DbCompiledModel 类被保存在内存中,因此经过一些搜索后,将其追踪到保留“InitializedDatabases”列表的 LazyInternalContext 类。
https://github.com/dotnet/ef6/blob/master/src/EntityFramework/Internal/LazyInternalContext.cs#L670
有没有办法阻止实体框架这样做?,如果这是“InitializeDatabaseAction”所暗示的,它不是代码优先设置,数据库设置和迁移不在此应用程序中完成。
设置“return null”或将“InitializerDisabled”设置为true会使一切正常,但宁愿不运行自定义实体构建,而且不知道仅“更改”源会产生什么影响。
大多数表都有相同的定义,所以也尝试了我在这里找到的解决方案: 在运行时更改表名
尝试此操作时,我收到错误消息“此命令存在开放数据读取器”,不支持使用 postgres 和 MARS(不知道为什么我需要它,这只会更改正在运行的 sql)
Ivan Stoev 的评论中给出了解决方案,并且有效。
如果不使用反射,则无法关闭此功能,将“InternalContext.InitializerDisabled”属性设置为 true 将使此功能跳过字典。
所以:
我用来测试这个的示例中的代码,作为测试设置,我有 1 个带有 30k 分区的主表,分区本身被查询,因为 postgres(特别是 9.x)不能很好地适应大量分区:
public class PartContext : DbContext {
private static readonly string _ConnectionString = new NpgsqlConnectionStringBuilder {
Host = "localhost",
Port = 5432,
Database = "postgres",
Username = "postgres",
Password = "password"
}.ConnectionString;
public readonly string Table;
public readonly string Partition;
public PartContext(string pMainTable, string pPartition) : base(
new NpgsqlConnection() { ConnectionString = _ConnectionString },
PartDbModelBuilder.Get(_ConnectionString, pPartition),
true
) {
Table = pMainTable;
Partition = pPartition;
Database.SetInitializer<PartContext>(null);
/**
* Disable database initialization so that the DbCachedModels are not kept internally in Entity
* This causes high memory usage when having a lot of tables
* In EF 6.4.2 there was no way to 'manage' that Dictionary externally
*/
try {
var InternalContext = typeof(PartContext).BaseType.GetProperty("InternalContext", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(this, null);
InternalContext.GetType().GetProperty("InitializerDisabled").SetValue(InternalContext, true);
} catch(Exception) { }
}
public DbSet<MyPart> Parts { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.HasDefaultSchema("public");
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
Run Code Online (Sandbox Code Playgroud)
这提供了 DbCachedModel:
我建议添加一些自定义缓存代码等,这只是一个示例
class PartDbModelBuilder {
public static DbCompiledModel Get(string pConnectionString, string pTable) {
DbModelBuilder builder = new DbModelBuilder();
builder.Entity<MyPart>().ToTable(pTable, "public");
using (var connection = new NpgsqlConnection() { ConnectionString = pConnectionString }) {
var obj = builder.Build(connection).Compile();
return obj;
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我用作测试的实体:
public class MyPart {
public int id { get; set; }
public string name { get; set; }
public string value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
I 类用于运行测试:
class EFTest {
public void Run(int tableCount) {
int done = 0;
Parallel.For(0, tableCount, new ParallelOptions { MaxDegreeOfParallelism = 5 }, (i) => {
string id = i.ToString().PadLeft(5, '0');
using (var context = new PartContext("mypart", "mypart_" + id)) {
var objResult = context.Parts.First();
Console.WriteLine(objResult.name);
}
done++;
Console.WriteLine(done + " DONE");
});
}
}
Run Code Online (Sandbox Code Playgroud)
表定义:
CREATE TABLE IF NOT EXISTS mypart (
id SERIAL,
name text,
value text
) partition by list (name);
CREATE TABLE IF NOT EXISTS part partition of mypart_00000 for values in ('mypart00000');
CREATE TABLE IF NOT EXISTS part partition of mypart_00001 for values in ('mypart00001');
CREATE TABLE IF NOT EXISTS part partition of mypart_00002 for values in ('mypart00002');
...
Run Code Online (Sandbox Code Playgroud)
Postgres 9:
CREATE TABLE IF NOT EXISTS mypart (
id SERIAL,
name text,
value text
);
CREATE TABLE IF NOT EXISTS ".$name."( CHECK ( name = 'mypart00000')) INHERITS (mypart);
CREATE TABLE IF NOT EXISTS ".$name."( CHECK ( name = 'mypart00001')) INHERITS (mypart);
CREATE TABLE IF NOT EXISTS ".$name."( CHECK ( name = 'mypart00002')) INHERITS (mypart);
...
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
230 次 |
| 最近记录: |