Entity Framework data generation option databasegenerated "Go"

Source: Internet
Author: User

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

public class person    {        [Key] public        Guid SocialSecurityNumber {get; set;}        public string FirstName {get; set;}        public string LastName {get; set;}    }
            var person = new Person            {                FirstName = "Rowan",                LastName = "Miller",            };            using (var context = new Breakawaycontext ())            {                context. People.add (person);                Context. SaveChanges ();            }

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

public class person    {        [key,databasegenerated (databasegeneratedoption.identity)] public        Guid SocialSecurityNumber {get; set;}        public string FirstName {get; set;}        public string LastName {get; set;}    }

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

public class person    {        [Key] public        int SocialSecurityNumber {get; set;}        public string FirstName {get; set;}        public string LastName {get; set;}    }

Let's insert a record and see

   var person = new Person            {                FirstName = "Rowan",                LastName = "Miller",                SocialSecurityNumber = 12345678< c13/>};            using (var context = new Breakawaycontext ())            {                context. People.add (person);                Context. SaveChanges ();            }

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.

public class person    {        [Key, Databasegenerated (databasegeneratedoption.none)] public        int SocialSecurityNumber {get; set;}        public string FirstName {get; set;}        public string LastName {get; set;}    }

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.

  public class person    {        [Key, Databasegenerated (databasegeneratedoption.none)] public        int SocialSecurityNumber {get; set;}        public string FirstName {get; set;}        public string LastName {get; set;}        [Databasegenerated (databasegeneratedoption.computed)]        public string Name {get; set;}    }
var person = new Person            {                FirstName = "Rowan",                LastName = "Miller",                socialsecuritynumber = 1231478,                Name = "Rowan Miller",            };            using (var context = new Breakawaycontext ())            {                context. People.add (person);                Context. SaveChanges ();            }

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

Entity Framework data generation option databasegenerated "Go"

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.