Barefoot learning LINQ (043): Specifies the data type in the database for column members of the object class

Source: Internet
Author: User
Video Demonstration: u.115.comfilef21113c26 focuses on specifying the Data Type of the column member in the database by setting the DbType attribute for the ColumnAttribute feature of the object class member. The attribute value assigned to DbType is a string. This string can contain the data type, data size, and whether it is

Video demo: http://u.115.com/file/f21113c26 demo focus by setting the DbType attribute for the ColumnAttribute attribute of the entity class member, you can specify its data type in the database for the column member. The attribute value assigned to DbType is a string. This string can contain the data type, data size, and whether it is

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

Demonstration highlights
By setting the DbType attribute for the ColumnAttribute attribute of object class members,
You can specify the Data Type of the column member in the database.
The attribute value assigned to DbType is a string.
This string can contain the data type, data size, and whether it is null.
For example, ColumnAttribute. DbType = "NVarChar (40) not null"
In fact, this attribute is useful only when we create a database based on the object model.
That is, when the data context DataContext. CreateDatabase () method is called.
When this method is called, it reads the DbType attribute value and then determines the Data Type of the column in the created data table.
When you use the object model to add, delete, query, and modify data, it seems that this attribute is useless.

It also demonstrates how to create a database through an object model.
The following three methods are involved:
DataContext. DatabaseExists (): determines whether the specified database already exists.
DataContext. DeleteDatabase (): deletes a specified database.
DataContext. CreateDatabase (): creates a database based on the object model.
When you create a database using an object model, if the ColumnAttribute. DbType attribute is not set for the column members of the object class,
NVarchar (4000) is used as the default data type.
For this reason, if we want to use the object model to create a database,
It is better to explicitly set the ColumnAttribute. DbType of the object class column member.
It must be set to a valid type. Otherwise, the create table command generated by LINQ to SQL contains invalid data types,
Naturally, it will not be executed successfully.
However, if you do not need to use the object model to create a database, you can set this attribute. Pai_^

Key code
The following code demonstrates how to specify the data type in the database for the column Member of the object class, focusing on the DbType attribute.
[Column (Storage = "_ ContactTitle", DbType = "NVarChar (30)")] public string ContactTitle {get {return this. _ ContactTitle;} set {if (this. _ ContactTitle! = Value) {this. onContactTitleChanging (value); this. sendPropertyChanging (); this. _ ContactTitle = value; this. sendPropertyChanged ("ContactTitle"); this. onContactTitleChanged ();}}}

The following code demonstrates how to create a database based on the object model of the LINQ to SQL statement.
NorthwindDataContext db = new NorthwindDataContext (@ "C:/LINQ/NorthwindExpress. mdf "); db. log = Console. out; if (db. databaseExists () db. deleteDatabase (); db. createDatabase ();
Study records
This code is mainly used to study the ColumnAttribute. DbType attribute.
This attribute is especially important when you want to use the DataContext. CreateDtabase () method to create a database based on the object model.
If this attribute is not set or the attribute value is invalid, the database cannot be created correctly?
When an invalid data type is set for the DbType attribute, LINQ to SQL cannot determine whether it is valid.
It simply combines the DbType string attribute values into an SQL command for creating a database,
When this command is sent to the database, it cannot be correctly executed because the data type of a column is invalid.
From this point of view, there are still some stupid points about the use of LINQ to SQL. -_-
If the DbType attribute is not set, the default data type is NVarChar (4000.
Look at this. I think we should specify the Data Type clearly!
If the object model is not used to create a database, but only to operate the data in the created database,
Therefore, the DbType attribute value cannot be set for the column attribute.
This does not affect the use effect.
However, if you want to create a database based on the object model, you 'd better specify the Data Type explicitly!

Barefoot

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.