Entity Framework data generation options databasegenerated

Source: Internet
Author: User

Entity Framework data generation options databasegenerated

In EF, when we build the data model, we can give the property configuration data Generation option databasegenerated, which has three enumerated values: Identity, none, and computed.

Identity: Self-growth

None: Do not process

Computed: Indicates that this column is a computed column.

In EF, if the primary key is of type int, Code first will automatically set the column self-growth when it generates the database. But if the primary key is a GUID type, we will have to set it manually.

For the following model, if we do not set self-growth, the database will be filled with zeros

Guid SocialSecurityNumber {;   FirstName {; LastName {;

person = = = (Context =

When you insert a second record, you get an error. Therefore, the following model settings are correct.

Guid SocialSecurityNumber {;   FirstName {; LastName {;

Now let's take a look at some scenarios where configuring none is also useful. Modify the model above.

SocialSecurityNumber {;   FirstName {; LastName {;

Let's insert a record and see

person = = = = (Context =

stored in the database is 1, not what we want 12345678. What is this for?

Because the primary key is SocialSecurityNumber is the int type, Code first does the self-growth processing of the column in the database. At this point, we would like to insert a custom socialsecuritynumber.

So when we want to do this, we should configure the SocialSecurityNumber self-growth to none.

SocialSecurityNumber {;   FirstName {; LastName {;

If the attribute is identified as COMPUTED,EF, the column is considered to be calculated from a different column and is not persisted to the database.

SocialSecurityNumber {;   FirstName {;   LastName {; Name {;

person = = = = = (Context =

Looking at the database, we see that name does not store any values.

Entity Framework data generation options databasegenerated

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.