【问题标题】:Linq2db entity framework update query for dynamic table and columnLinq2db 实体框架更新查询动态表和列
【发布时间】:2020-04-07 13:50:06
【问题描述】:

我想要使用 linq2db.EntityFrameworkCore 对所有表进行通用查询

我有 5 件事。 1. 表名 2. 列名 3. 列值 4. where条件列名 5.where条件列值。

到目前为止,我实际上正在尝试的内容如下所示。

public void update(string entity, string attribute, object value, string whereAttribute, string whereAttributeValue)
{
    projectContext.Set<object>().ToLinqToDBTable().TableName(entity)
        .Where(t => t[whereAttribute] == whereAttributeValue) // This is not working.
        .Set(t => t[attribute], value) // so far it is not giving any build error.
        .Update();
}

但它不起作用。如何解决这个问题?

【问题讨论】:

    标签: c# entity-framework linq entity-framework-core linq2db


    【解决方案1】:

    Linq2db 具有动态属性功能,您可能可以在这里使用,但它需要知道实体类型:

    public void update<TEntity>(string entity, string attribute, object value, string whereAttribute, string whereAttributeValue)
        where TEntity : class
    {
        projectContext.GetTable<TEntity>()
            .TableName(entity)
            .Where(t => Sql.Property<TEntity>(t, whereAttribute).Equals(whereAttributeValue))
            .Set(t => Sql.Property<TEntity>(t, attribute), value)
            .Update();
    }
    

    【讨论】:

    • 我没有 TEntity :(
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    相关资源
    最近更新 更多