如何将linq中的计算列映射到sql类 [英] how to map a computed column in linq to sql classes
问题描述
如何将计算列映射到我的LinqToSql类? MSDN告诉我使用expression属性,但是我不知道如何将公式转换为表达式.
How can i map a computed column to my LinqToSql Classes? MSDN tells me to use the expression property however i have no idea how to transform my formula into an expression.
我的问题是,一旦我使用SubmitChanges()创建行,除我对象中的计算列外,每个属性都会更新.在其中我需要用于其他任务.
My Problem is that once i create the row using SubmitChanges() every property updates except my computed columns in my object. In which i needed to use for another task.
该公式使用标量sql函数进行计算,在该公式中,该公式计算另一个表中的值之和.我可以转换它,还是必须创建自己的未映射属性来为我计算该属性,还是刷新我的datacontext以获取值?
The formula uses a scalar sql function to compute it, in which it computes the sum of values from another table. Can i convert it or do i have to create my own un-mapped property that computes it for me or refresh my datacontext to get the values?
推荐答案
使用Expression
属性,您可以轻松映射计算列,例如...
By using the Expression
attribute you can easily map computed columns, for example...
[Table(Name = "Orders")]
class Order
{
[Column(Storage="Price", DbType="Money",Expression="UnitPrice + 1.00")]
public decimal Price{ get; set }
[Column(Storage="Qty", DbType="Int"]
public int Quantity { get; set; }
[Column(Storage="Total", DbType="Money",Expression="Price * Quantity")]
public decimal Total { get; set; }
}
这篇关于如何将linq中的计算列映射到sql类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!