LINQ-to-SQL:ExecuteQuery(Type, String) 填充一个字段,但不填充另一个字段
我编写了一个应用程序,用作代理从数据库查询数据并将其自动加载到我的分布式 Web 缓存中。
我通过在配置中指定 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()
),并且 < code>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”属性。 我尝试自己运行查询,并返回正确的列名称,并且列名称与我在映射属性中指定的名称相匹配。 帮助!
I've written an app that I use as an agent to query data from a database and automatically load it into my distributed web cache.
I do this by specifying an sql query and a type in a configuration. The code that actually does the querying looks like this:
List<Object> result = null;
try { result = dc.ExecuteQuery(elementType, entry.Command).OfType<Object>().ToList(); }
catch (Exception ex) { HandleException(ex, WebCacheAgentLogEvent.DatabaseExecutionError); continue; }
elementType
is a System.Type created from the type specified in the configuration (using Type.GetType()
), and entry.Command
is the SQL query.
The specific entity type I'm having an issue with looks like this:
public class FooCount
{
[Column(Name = "foo_id")]
public Int32 FooId { get; set; }
[Column(Name = "count")]
public Int32 Count { get; set; }
}
The SQL query looks like this:
select foo_id as foo_id, sum(count) as [count]
from foo_aggregates
group by foo_id
order by foo_id
For some reason, when the query is executed, the "Count" property ends up populated, but not the "FooId" property. I tried running the query myself, and the correct column names are returned, and the column names match up with what I've specified in my mapping attributes. Help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这太疯狂了......
解决我的问题的是用
TableAttribute
装饰我的实体类:我假设(显然是错误的),因为我没有使用
GetTable()
方法,我不需要相应的映射属性。更新:一年半后,我终于意识到,除非类上有相应的 TableAttribute 装饰,否则属性上的 ColumnAttribute 装饰似乎会被忽略。 这解释了为什么“Count”属性被填充,因为它的命名将与 SQL 语句中的列匹配,而 FooId/foo_id 当然不匹配。
This is insane...
What fixed my problem was decorating my entity class with
TableAttribute
:I had assumed (wrongly, apparently) that since I wasn't using the
GetTable<T>()
method, I didn't need the corresponding mapping attribute.Update: A year and a half later, it finally dawned on me it seems like the ColumnAttribute decorations on the properties are ignored unless there's a corresponding TableAttribute decoration on the class. This explains why the "Count" property was getting populated, since its naming would match the column in the SQL statement, whereas FooId/foo_id of course do not match.
当属性名称与列名称不同时,Linq To Sql 很难映射内容。 尝试将属性名称更改为带下划线的 foo_id 。 这应该是诀窍。
或者,您也可以将 select 语句更改为 foo_id 作为 FooId 以匹配您的属性。 不管怎样,它们应该是相同的(但不需要是相同的情况)。
Linq To Sql has a hard time mapping stuff when the names of the properties are different than the names of the columns. Try changing your property name to foo_id with the underscore. That should to the trick.
Either that, or you can change your select statement to foo_id as FooId to match your property. Either way, they should be the same (don't need to be the same case though).