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.