How to represent calculated columns (LINQ to SQL)

Source: Internet
Author: User

Http://u.115.com/file/f23bcc71be demo

Demonstration highlights
Add the expression attribute to the columnattribute attribute of the object class column member,
Then, it indicates the computed columns corresponding to the data table.
This attribute is of the string type,
The value assigned to this attribute is the expression of the computed column in the data table.
For example, expression = "[unitprice] * [unitsinstock]"
By default, the O/R designer does not set this attribute for the corresponding column member based on the definition of the data table.

What are the functions of the calculated columns in the object model of LINQ to SQL?
This can be viewed in two situations:
① When the object model is not used to create a database:
In this case, we actually use LINQ to SQL to execute some conventional database operations,
In this case, the expression attribute is optional and does not have much impact.
However, if this attribute is included, when you use LINQ to SQL to update or add data records,
The updated calculated column value is automatically obtained.
This can be learned from the SQL commands generated by LINQ to SQL.
② When you create a database using an object model:
That is, call datacontext. createdatabase () to create a database,
At this time, the role of this attribute is crucial.
It not only determines whether the corresponding data column is a computed column,
It also specifies the calculation expression of the computed column.
Therefore, in this case, if expression is not set,
It is estimated that it is not easy to generate a data table containing a valid calculated column?

Not important
If columnattribute. expression is set for the column Member of the object class,
There is no need to set columnattribute. dbtype. Even if you do not set it, no error will occur.
If you set it, it seems unreasonable.
Since it is calculated by the database to obtain the value, it is really hard to say what kind of data range it will be.
Do you think it will be reasonable if you just set it in an instant?
Also, if a column member is a calculated column, do not set it as a primary key column.
Although this is usually allowed,
However, if you use an object model to create a database,
You have to generate an exception.
According to the exception information, the calculated column is not set as a persistent column, and is likely to be null.
Such columns naturally do not meet the requirements of primary key columns.

Key code
In the following code, set the totalvalue attribute of the object class to the computed column.
The formula is [unitprice] * [unitsinstock].

[Column(Storage="_TotalValue",    DbType="Money",    UpdateCheck=UpdateCheck.Never,    Expression="[UnitPrice] * [UnitsInStock]")]public System.Nullable<decimal> TotalValue{ get {  return this._TotalValue; } set {  if ((this._TotalValue != value))  {   this.OnTotalValueChanging(value);   this.SendPropertyChanging();   this._TotalValue = value;   this.SendPropertyChanged("TotalValue");   this.OnTotalValueChanged();  } }}

Barefoot thinking

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.