Entity Framework Guid Primary Key Auto Generated
Dec 22, 2016 Hi, I want to create Auto generated column in Entity framework 6 using Code first approach public class Shopping DatabaseGenerated(DatabaseGeneratedOption.Identity) Key public int.
Sep 14, 2017 Gotcha inserting entities with explicit ID generated in database in Entity Framework Core 2 compared to Entity Framework 6. 14 Sep 2017 Entity Framework, Entity Framework Core, MS SQL Server Yesterday I was rewriting some code from Entity Framework 6 to Entity Framework 2 for testing and discovered unexpected behavior. Hi, I want to create Auto generated column in Entity framework 6 using Code first approach public class Shopping DatabaseGenerated(DatabaseGeneratedOption.Identity) Key public int ProductId. A key serves as a unique identifier for each entity instance. Most entities in EF have a single key, which maps to the concept of a primary key in relational databases (for entities without keys, see Keyless entities). Entities can have additional keys beyond the primary key (see Alternate Keys for more information). GUIDs may seem to be a natural choice for your primary key - and if you really must, you could probably argue to use it for the PRIMARY KEY of the table. What I'd strongly recommend not to do is use the GUID column as the clustering key, which SQL Server does by default, unless you specifically tell it not to. You really need to keep two issues. This article describes the effect of Entity Framework Code First convention and configuration for creating Primary Key column. Entity Framework requires each entity to have a key. By using this key, Entity Framework keeps track of the changes that happens to that entity. Listed below are three different ways to create primary key using Code First.
-->A key serves as a unique identifier for each entity instance. Most entities in EF have a single key, which maps to the concept of a primary key in relational databases (for entities without keys, see Keyless entities). Entities can have additional keys beyond the primary key (see Alternate Keys for more information).
By convention, a property named Id
or <type name>Id
will be configured as the primary key of an entity.
Note
Owned entity types use different rules to define keys.
You can configure a single property to be the primary key of an entity as follows:
You can also configure multiple properties to be the key of an entity - this is known as a composite key. Composite keys can only be configured using the Fluent API; conventions will never setup a composite key, and you can not use Data Annotations to configure one.
Primary key name
By convention, on relational databases primary keys are created with the name PK_<type name>
. You can configure the name of the primary key constraint as follows:
Key types and values
While EF Core supports using properties of any primitive type as the primary key, including string
, Guid
, byte[]
and others, not all databases support all types as keys. In some cases the key values can be converted to a supported type automatically, otherwise the conversion should be specified manually.
Key properties must always have a non-default value when adding a new entity to the context, but some types will be generated by the database. In that case EF will try to generate a temporary value when the entity is added for tracking purposes. After SaveChanges is called the temporary value will be replaced by the value generated by the database.
Important
If a key property has its value generated by the database and a non-default value is specified when an entity is added, then EF will assume that the entity already exists in the database and will try to update it instead of inserting a new one. To avoid this turn off value generation or see how to specify explicit values for generated properties.
Alternate Keys
An alternate key serves as an alternate unique identifier for each entity instance in addition to the primary key; it can be used as the target of a relationship. When using a relational database this maps to the concept of a unique index/constraint on the alternate key column(s) and one or more foreign key constraints that reference the column(s).
Tip
If you just want to enforce uniqueness on a column, define a unique index rather than an alternate key (see Indexes). In EF, alternate keys are read-only and provide additional semantics over unique indexes because they can be used as the target of a foreign key.
Alternate keys are typically introduced for you when needed and you do not need to manually configure them. monotype corsiva download free mac By convention, an alternate key is introduced for you when you identify a property which isn't the primary key as the target of a relationship.
You can also configure a single property to be an alternate key:
You can also configure multiple properties to be an alternate key (known as a composite alternate key):
Finally, by convention, the index and constraint that are introduced for an alternate key will be named AK_<type name>_<property name>
(for composite alternate keys <property name>
becomes an underscore separated list of property names). You can configure the name of the alternate key's index and unique constraint:
C# Entity Framework Example
-->Value generation patterns
There are three value generation patterns that can be used for properties:
- No value generation
- Value generated on add
- Value generated on add or update
No value generation
No value generation means that you will always supply a valid value to be saved to the database. This valid value must be assigned to new entities before they are added to the context.
Value generated on add
Value generated on add means that a value is generated for new entities.
Depending on the database provider being used, values may be generated client side by EF or in the database. If the value is generated by the database, then EF may assign a temporary value when you add the entity to the context. This temporary value will then be replaced by the database generated value during SaveChanges()
.
If you add an entity to the context that has a value assigned to the property, then EF will attempt to insert that value rather than generating a new one. A property is considered to have a value assigned if it is not assigned the CLR default value (null
for string
, 0
for int
, Guid.Empty
for Guid
, etc.). For more information, see Explicit values for generated properties.
Warning
How the value is generated for added entities will depend on the database provider being used. Database providers may automatically setup value generation for some property types, but others may require you to manually setup how the value is generated.
For example, when using SQL Server, values will be automatically generated for GUID
properties (using the SQL Server sequential GUID algorithm). However, if you specify that a DateTime
property is generated on add, then you must setup a way for the values to be generated. One way to do this, is to configure a default value of GETDATE()
, see Default Values.
Value generated on add or update
Value generated on add or update means that a new value is generated every time the record is saved (insert or update).
Like value generated on add
, if you specify a value for the property on a newly added instance of an entity, that value will be inserted rather than a value being generated. It is also possible to set an explicit value when updating. For more information, see Explicit values for generated properties.
Warning
How the value is generated for added and updated entities will depend on the database provider being used. Database providers may automatically setup value generation for some property types, while others will require you to manually setup how the value is generated.
For example, when using SQL Server, byte[]
properties that are set as generated on add or update and marked as concurrency tokens, will be setup with the rowversion
data type - so that values will be generated in the database. However, if you specify that a DateTime
property is generated on add or update, then you must setup a way for the values to be generated. One way to do this, is to configure a default value of GETDATE()
(see Default Values) to generate values for new rows. You could then use a database trigger to generate values during updates (such as the following example trigger).
Value generated on add
By convention, non-composite primary keys of type short, int, long, or Guid are set up to have values generated for inserted entities, if a value isn't provided by the application. Your database provider typically takes care of the necessary configuration; for example, a numeric primary key in SQL Server is automatically set up to be an IDENTITY column.
You can configure any property to have its value generated for inserted entities as follows:
Warning
This just lets EF know that values are generated for added entities, it does not guarantee that EF will setup the actual mechanism to generate values. See Value generated on add section for more details.
Default values
On relational databases, a column can be configured with a default value; if a row is inserted without a value for that column, the default value will be used.
You can configure a default value on a property:
You can also specify a SQL fragment that is used to calculate the default value:
Dragon naturally speaking activation key generator version 10. Specifying a default value will implicitly configure the property as value generated on add.
Value generated on add or update
Warning
This just lets EF know that values are generated for added or updated entities, it does not guarantee that EF will setup the actual mechanism to generate values. See Value generated on add or update section for more details.
Using Guids As Primary Key
Computed columns
On some relational databases, a column can be configured to have its value computed in the database, typically with an expression referring to other columns:
Note
In some cases the column's value is computed every time it is fetched (sometimes called virtual columns), and in others it is computed on every update of the row and stored (sometimes called stored or persisted columns). This varies across database providers.
No value generation
Disabling value generation on a property is typically necessary if a convention configures it for value generation. For example, if you have a primary key of type int, it will be implicitly set configured as value generated on add; you can disable this via the following: