Entity Framework data generation options DatabaseGenerated, entityframework

Source: Internet
Author: User

Entity Framework data generation options DatabaseGenerated, entityframework

Reference page:

Http://www.yuanjiaocheng.net/entity/Persistence-in-EF.html

Http://www.yuanjiaocheng.net/entity/crud-in-connected.html

Http://www.yuanjiaocheng.net/entity/crud-in-Disconnected.html

Http://www.yuanjiaocheng.net/entity/add-entity-in-disconnected.html

Http://www.yuanjiaocheng.net/entity/update-entity.html

In EF, when we create a data model, we can configure the data generation option DatabaseGenerated for the attribute, which has three enumerated values: Identity, None, and Computed.

Identity: self-growth

None

Computed: indicates that this column is a calculation column.

In EF, if the primary key is of the int type, the column is automatically set to auto-increment when Code First generates the database. However, if the primary key is of the Guid type, we need to manually set it.

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

 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 the second record is inserted, an error is reported. 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 situations where the configuration of 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 another record.

   var person = new Person            {                FirstName = "Rowan",                LastName = "Miller",                SocialSecurityNumber = 12345678            };            using (var context = new BreakAwayContext())            {                context.People.Add(person);                context.SaveChanges();            }

1 is stored in the database, not 12345678 we want. Why?

Because the primary key is SocialSecurityNumber and Int type, Code First processes the column in the database as auto-increment. In this case, we cannot insert a custom SocialSecurityNumber.

So when we want to do this, we should set the auto-Growth of SocialSecurityNumber 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 considers the column to be calculated by other columns and does not persistently store it in 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();            }

View the database. We can see that the Name does not store any value.

 

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.