如何将linq中的计算列映射到sql类中?[英] how to map a computed column in linq to sql classes

本文是小编为大家收集整理的关于如何将linq中的计算列映射到sql类中?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

如何将计算列映射到我的 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; }
}

本文地址:https://www.itbaoku.cn/post/1557107.html

问题描述

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; }
}