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

发布于 2024-07-14 09:36:38 字数 958 浏览 13 评论 0原文

我编写了一个应用程序,用作代理从数据库查询数据并将其自动加载到我的分布式 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笔芯 2024-07-21 09:36:38

这太疯狂了......

解决我的问题的是用 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() 方法,我不需要相应的映射属性。

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

This is insane...

What fixed my problem was decorating my entity class with 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; }
}

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.

抱着落日 2024-07-21 09:36:38

当属性名称与列名称不同时,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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文