A Ka Sup
A Ka Sup
CC#
Created by A Ka Sup on 9/6/2023 in #help
❔ Cannot change table from second call
hello, according to this document https://learn.microsoft.com/en-us/ef/core/modeling/dynamic-model, i want to get dynamic model so i create a class that inherited from IModelCacheKeyFactory
public class CustomModelCacheKeyFactory : IModelCacheKeyFactory {
public object Create(DbContext context) => new CustomModelCacheKey(context);
}

public class CustomModelCacheKey {
(
Type ContextType,
string TB1TableName,
string TB2TableName
) key;
public CustomModelCacheKey(DbContext context) {
key.ContextType = context.GetType();
key.TB1TableName = (context as DatabaseContext)?.TB1TableName;
key.TB2TableName = (context as DatabaseContext)?.TB2TableName;
}
public override int GetHashCode() => key.GetHashCode();
public override bool Equals(object obj) => obj is CustomModelCacheKey other && key.Equals(other.key);
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
optionsBuilder.ReplaceService<IModelCacheKeyFactory, CustomModelCacheKeyFactory>();
}
public class CustomModelCacheKeyFactory : IModelCacheKeyFactory {
public object Create(DbContext context) => new CustomModelCacheKey(context);
}

public class CustomModelCacheKey {
(
Type ContextType,
string TB1TableName,
string TB2TableName
) key;
public CustomModelCacheKey(DbContext context) {
key.ContextType = context.GetType();
key.TB1TableName = (context as DatabaseContext)?.TB1TableName;
key.TB2TableName = (context as DatabaseContext)?.TB2TableName;
}
public override int GetHashCode() => key.GetHashCode();
public override bool Equals(object obj) => obj is CustomModelCacheKey other && key.Equals(other.key);
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
optionsBuilder.ReplaceService<IModelCacheKeyFactory, CustomModelCacheKeyFactory>();
}
If i want to change table, i just call
_context.TB1TableName = "TB1_Other";
_context.TB1TableName = "TB1_Other";
and when i call _context.Set<TB1>() or _context.Database.GetDbConnection(), the CustomModelCacheKey will run and the table will be changed from TB1 to TB1_Other. Basically, it works perfectly. But now the problem is, when i do this
_context.TB1TableName = "TB1_Other";
var tb1 = _context.Set<TB1>();
_context.TB2TableName = "TB2_Other";
var tb2 = _context.Set<TB2>();
_context.TB1TableName = "TB1_Other";
var tb1 = _context.Set<TB1>();
_context.TB2TableName = "TB2_Other";
var tb2 = _context.Set<TB2>();
The CustomModelCacheKey will be fired only once on line var tb1 = _context.Set<TB1>(); , and not on line var tb2 = _context.Set<TB2>(); . So, from model TB1 and TB2, i will get data from table TB1_OTher and TB2 How can i change table from second line var tb2 = _context.Set<TB2>(); like above
6 replies