1

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?

blinkThrice
  • 305
  • 1
  • 2
  • 9

1 Answers1

2

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; }
}
Leo
  • 14,625
  • 2
  • 37
  • 55
  • Is it possible to place methods in the expression or does it only works if you place property + operators? – blinkThrice Jan 22 '16 at 04:51
  • Yep, absolutely. Check this code snippet from MSDN...https://msdn.microsoft.com/en-us/library/system.data.linq.mapping.columnattribute.expression(v=vs.100).aspx – Leo Jan 22 '16 at 04:54