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"