0

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

我有 5 件事。1. TableName 2. ColumnaName 3. ColumnValue 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();
}

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

4

1 回答 1

2

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();
}
于 2020-04-08T08:15:20.350 回答