问题描述
如何将计算列映射到我的 LinqToSql 类?MSDN 告诉我使用表达式属性,但是我不知道如何将公式转换为表达式.
我的问题是,一旦我使用 SubmitChanges() 创建行,每个属性都会更新,但对象中的计算列除外.我需要在其中使用其他任务.
该公式使用标量 sql 函数来计算它,其中它计算来自另一个表的值的总和.我可以转换它还是必须创建自己的未映射属性来为我计算它或刷新我的数据上下文以获取值?
推荐答案
通过使用 Expression 属性,您可以轻松映射计算列,例如...
[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; } }
问题描述
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.
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.
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?
推荐答案
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; } }