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