Just please refer the entityframework and do this by referring the namespace of System.ComponentModel.DataAnnociations and then add the attribute onto the ItemId
public class Item
{
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity),Key()]
public int ItemID { get; set; }
}
Using the data annotation attributes with the ADO.NET Entity Framework affects how the ADO.NET Entity Framework generates the database schema and performs validation of values when the data is saved using
DbContext.SaveChanges. However, using the DbModelBuilder only changes the database schema. Which approach you choose can change the error messages you see when invalid data is submitted as well as determine whether a database call is made.
Eagle_f90
Member
465 Points
531 Posts
EF5 Code First, making an identity column
Nov 23, 2012 03:55 PM|LINK
I have the following in my class and need to know how to modify the DataAnnotation to make it an identity column
public class Item { public Int16 ItemID { get; set; } }The goal is to have the EF5.0 equivalant of the SQL
ignatandrei
All-Star
134511 Points
21576 Posts
Moderator
MVP
Re: EF5 Code First, making an identity column
Nov 24, 2012 03:30 AM|LINK
http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.schema.databasegeneratedattribute%28v=vs.95%29.aspx
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: EF5 Code First, making an identity column
Nov 24, 2012 04:21 AM|LINK
Hello,
Just please refer the entityframework and do this by referring the namespace of System.ComponentModel.DataAnnociations and then add the attribute onto the ItemId
public class Item { [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity),Key()] public int ItemID { get; set; } }thaicarrot
Contributor
5120 Points
1459 Posts
Re: EF5 Code First, making an identity column
Nov 25, 2012 08:37 AM|LINK
You'd better forgoes the Data Annotation.
Using the data annotation attributes with the ADO.NET Entity Framework affects how the ADO.NET Entity Framework generates the database schema and performs validation of values when the data is saved using DbContext.SaveChanges. However, using the DbModelBuilder only changes the database schema. Which approach you choose can change the error messages you see when invalid data is submitted as well as determine whether a database call is made.
private void SetupVehicleEntity(DbModelBuilder modelBuilder) { modelBuilder.Entity<UEntity>().HasKey(u => u.EntityId); modelBuilder.Entity<UEntity>().Property(u => u.EntityId) .HasDatabaseGeneratedOption( DatabaseGeneratedOption.Identity); }Weera