【问题标题】:Is there a dynamic way to specify table in Entity Framework Core in .Net 6.Net 6中的Entity Framework Core中是否有一种动态的方法来指定表
【发布时间】:2021-11-22 09:54:18
【问题描述】:

是否有更简洁的方法可以从 Entity Framework 的上下文中动态选择表?也许在 for next 循环中?如果还有其他可能,但看起来很乱,我们需要处理大约 10 张桌子。表中的所有字段名称都相同。我们已经在使用 .Net 6 和 VS 2022。因此,如果有更新的功能,我会支持它。

await using timeFrameContext context = new();

var existsAlready = await context.FiveMin
    .AnyAsync(t => t.ProductName == symbol).ConfigureAwait(false);

var existsAlready = await context.FifteenMin
    .AnyAsync(t => t.ProductName == symbol).ConfigureAwait(false);

var existsAlready = await context.ThirtyMin
    .AnyAsync(t => t.ProductName == symbol).ConfigureAwait(false);

等等……

【问题讨论】:

  • DbContext.Set<TEntity>()?

标签: c# .net-core entity-framework-core


【解决方案1】:

你需要这样的东西吗?

public async Task<bool> exist<T>(Expression<Func<T, bool>> condition) where T : class
{
    return await _contextDb.Set<T>().AnyAsync(condition);
}

那么你可以这样调用

var a = await existAsync((FiveMin t) => t.ProductName == symbol);
var b = await existAsync((FifteenMin t) => t.ProductName == symbol);
var c = await existAsync((ThirtyMin t) => t.ProductName == symbol);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-14
    • 2014-06-12
    • 2019-03-09
    • 2022-08-20
    • 2017-04-04
    • 1970-01-01
    • 2017-01-09
    相关资源
    最近更新 更多