C
C#9mo ago
A Ka Sup

❔ 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
3 Replies
WEIRD FLEX
WEIRD FLEX9mo ago
i had the need for using some tables with more than one model, in the end i just created more db contexts, it was the cleanest way anyway do you actually need to switch more than one table name in the same context? also what version are you using, to me Create requires dbcontext and bool
A Ka Sup
A Ka Sup9mo ago
in some apis, i have to get data from other tables like TB1_2023, TB1_2024, ... i mean TB1's entire data is divided into tables that classified by year. And all of these tables have same model structure. i'm using foreach loop, yeah, i need to change table in each loop, like from TB1_2023 to TB1_2024, but if i do as like above example, it's not working in the end, i had to create scoped context using var context = ... to get data from other tables by the way, i'm using EFCore 3.1, so Create doesn't require bool
Accord
Accord9mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.