gpt4 book ai didi

c# - LINQ-to-SQL:ExecuteQuery(Type, String) 填充一个字段,但不填充另一个字段

转载 作者:太空狗 更新时间:2023-10-29 23:04:47 26 4
gpt4 key购买 nike

我编写了一个应用程序,用作从数据库查询数据并自动将其加载到我的分布式网络缓存中的代理。

我通过在配置中指定一个 sql 查询和一个类型来做到这一点。实际执行查询的代码如下所示:

List<Object> result = null;
try { result = dc.ExecuteQuery(elementType, entry.Command).OfType<Object>().ToList(); }
catch (Exception ex) { HandleException(ex, WebCacheAgentLogEvent.DatabaseExecutionError); continue; }

elementType 是根据配置中指定的类型创建的 System.Type(使用 Type.GetType()),以及 entry.Command 是 SQL 查询。

我遇到问题的特定实体类型如下所示:

public class FooCount
{
[Column(Name = "foo_id")]
public Int32 FooId { get; set; }

[Column(Name = "count")]
public Int32 Count { get; set; }
}

SQL 查询如下所示:

select foo_id as foo_id, sum(count) as [count]
from foo_aggregates
group by foo_id
order by foo_id

出于某种原因,在执行查询时,“Count”属性最终会被填充,但“FooId”属性不会。我尝试自己运行查询,返回了正确的列名,并且列名与我在映射属性中指定的相匹配。帮助!

最佳答案

这太疯狂了......

解决我问题的方法是用 TableAttribute 装饰我的实体类:

[Table(Name = "foo_aggregates")]
public class FooCount
{
[Column(Name = "foo_id")]
public Int32 FooId { get; set; }

[Column(Name = "count")]
public Int32 Count { get; set; }
}

我曾假设(显然是错误的)因为我没有使用 GetTable<T>()方法,我不需要相应的映射属性。

更新:一年半之后,我终于明白了,似乎属性上的 ColumnAttribute 装饰被忽略了,除非类上有相应的 TableAttribute 装饰。这解释了为什么要填充“Count”属性,因为它的命名与 SQL 语句中的列匹配,而 FooId/foo_id 当然不匹配。

关于c# - LINQ-to-SQL:ExecuteQuery(Type, String) 填充一个字段,但不填充另一个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/591097/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com