Default Value of LINQ to SQL

Source: Internet
Author: User

In LINQ to SQLNull, But set the default field, the default generatedModelWillNullThe default value of the database is not used.

 

Which of the following are related to the default values?Attribute:

1.In columnattribute Isdbgenerated attributes :if isdbgenerated true , this attribute only uses the default value set in the database. Generally, the default value is getdate () the timestamp field of uses this attribute. Because the default value of the database is used when there is no value, rather than always using the default value of the database.

2.ColumnattributeInDbtypeAttribute

3.Canbenull attribute in columnattriull

 

Example:

[Column (storage = "_ nvarcharnotnull", dbtype = "nvarchar (50) not null", canbenull = false, Isdbgenerated = true)]

 

 

SetNot null, Automatically GeneratedCodeAs follows:

[Column (storage = "_ nvarcharnotnull", dbtype = "nvarchar (50) not null", canbenull = false)]

HoweverNewThis field is stillNullAndInsertTry to insertNullValue with an error:

NULL values cannot be inserted into the 'nvarcharnotnull 'column, and the 'demo. DBO. table1' Column cannot have null values. Insert failed.

This is an automatically generated codeBug.

Since the default value of the database cannot be used, try to set the default value of the field. SetDefaultvalueattribute:

[Column (storage = "_ nvarcharnotnull", dbtype = "nvarchar (50) not null", canbenull = false), Defaultvalue ("B")]

The property is invalid and the default value of the field is stillNull, Is anotherBug!

The only way is to use the initialization tool to set the default value of the private field: Private string _ nvarcharnotnull = "AAA ";

 

Summary:

The experiment shows that the default value of the database cannot be used. Only one field can use the default value set by the database.---The timestamp column of a value cannot be input. SetIsdbgenerated Attributes can be implemented.

the current solution is to manually set the default value of the database on the private field. To ensure that database fields are not inserted due to code negligence null value, setting the field default value is useless and will still be LINQ Insert the default value. You can only set this field to not null .

from: http://blog.sina.com.cn/s/blog_443469830100e1u2.html

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.