❔ 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
If i want to change table, i just call
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
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 above3 Replies
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
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 boolWas 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.