This is one of the questions that I get a lot from developers who are using code first approach for Entity Framework. "How to specify a class property to be Identity column in database?" If you look at various data annotation attributes that are available for class properties you do not find any that is named like Identity. This does not mean that entity framework does not allow mapping a class property to a field that is marked as Identity in database. After digging through these annotations, I found DatabaseGenerated attribute that allows you to set Identity column. The constructor of this attribute forces you to set atabaseGeneratedOption option. This enumeration has three values.
None |
Identity |
Computed |
The following code snippet shows how this attribute can be set on a class.
public class ProductStatus { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public int ProductId { get; set; } public Status Status { get; set; } }
Set Identity Column attribute for property in Entity Framework
How to plan CCSP Exam preparation
Develop a MongoDB pipeline to transform data into time buckets
Alert and Confirm pop up using BootBox in AngularJS
AngularJS Grouped Bar Chart and Line Chart using D3
How to lock and unlock account in Asp.Net Identity provider
2025 © Byteblocks, ALL Rights Reserved. Privacy Policy | Terms of Use