When you develop a Web site using the Entity Framework in. Net 3.5, you encounter a problem: When you add a record, you cannot use the default value of the database for datetime-type data.
The specific situation is this, my database has a users table, three fields, Id,username,createtime.
ID is int type, since growth, primary key
Username is a Nvchar (10) type and is not allowed to be empty
Createtime is a datetime field and is not allowed to be NULL, the default value is GETDATE ()
If you use the Entity Framework to generate entity classes, add data directly, and do not specify Createtime, an error occurs at SaveChanges:
using (linqdemoentities lde = new Linqdemoentities ())
{
Users user = new users
{
Name = "Admin"
};
Lde. Addtousers (user);
Lde. SaveChanges ();
}
After looking for a lot of data, we know that this is a bug in the eentities framework, and you need to add the storegeneratedpattern= "Identity" attribute to the edmx file, createtime field.
Find the edmx file, open it with the Text tool, find the corresponding field properties, add the Storegeneratedpattern= identity property, and then save the exit.
After that, the statement is executed again, and the default value is inserted into the database.