鉴于此表:
CREATE TABLE [Comments]
(
[Id] [int] IDENTITY(1, 1) NOT NULL,
[Text] [nvarchar](600) NOT NULL
)
Run Code Online (Sandbox Code Playgroud)
有了这个模型类:
[Table(Name="Comments")]
public class Comment
{
[Column(AutoSync = AutoSync.OnInsert, DbType = "Int NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
public int Id { get; set; }
[Column(DbType = "NVarChar(600) NOT NULL", CanBeNull = false)]
public string Text { get; set; }
public string ArbitraryText { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
ArbitraryText使用该ExecuteQuery方法时,DataContext是否可以填充属性:
var comments = db.ExecuteQuery<Comment>("select Id, [Text], 'hello' [ArbitraryText] from Comments");
Run Code Online (Sandbox Code Playgroud)
似乎实体映射算法忽略了任何未标记的属性ColumnAttribute,但还有另一种方法吗?
我不想自己做映射,但这看起来像我唯一的选择.
public class PlainOldCSharpObject
{
public int Id { get; set; }
public string Text { get; set; }
public string ArbitraryText { get; set; }
}
...
// DataContext correctly fills these objects
var pocos = db.ExecuteQuery<PlainOldCSharpObject>("select Id, [Text]...
Run Code Online (Sandbox Code Playgroud)
所以我目前的解决方案是在我的LINQ映射对象上有一个内部类,它保存我的聚合查询返回的额外数据.这是次优的,因为一些属性是重复的(例如,Id和Text).
据我所知还没有。也许您可以做一些肮脏的事情来结合来自 UDF 的数据,但除此之外,它还希望能够映射列。
(其中 UDF 仅返回任意文本和评论 ID)
var qry = from row in db.SomeUdfQuery(someArgs)
join comment in db.Comments
on row.Id equals comment.Id
select new {Comment = comment, row.ArbitraryText};
var comments = new List<Comment>();
foreach(var record in qry) {
record.Comment.ArbitraryText = record.ArbitraryText;
comments.Add(record.Comment);
}
return comments;
Run Code Online (Sandbox Code Playgroud)
或者 - 有一段时间我写了一些变体ExecuteQuery,如果您需要使用这种方法来做很多不同的事情,这可能会很有用……不过,就我个人而言,我可能会首先尝试回避这个问题。