Default value for Entity framework bug resolution

Source: Internet
Author: User
Keywords nbsp; default value user workaround
Tags data default default value framework key net site type

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.

Related Article

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.