EF Auto-increment a non primary column with a seed value

I have a Order table and it has OrderNumber column of type INT. Order table also has a Id column which is Primary key. I need to make OrderNumber column to autoincrement the values from a Seed value. Any help?

Thanks Anjan

Answers


You can assign auto-increment to any column, but I would not do that, because OrderNumber is definitely identity of your Order entity. You should use it as primary key:

[Key] // make it primary key and remove Id property
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int OrderNumber { get; set; }

Adding auto-increment with fluent api looks like:

modelBuilder.Entity<Order>()
   .Property(o => o.OrderNumber)
   .HasDatabaseGenerationOption(DatabaseGenerationOption.Identity);

If you want to create seed for OrderNumber then you should use custom database initializer and execute DBCC CHECKIDENT query

DBCC CHECKIDENT ('Orders', RESEED, YourSeedValue)

Need Your Help

XML into Associative Array using PHP

php xml arrays

Can anyone help with converting data from an XML document into an associative array? I'm running into issues given that the XML structure is sort of 3D and the array is more of a 2D structure (please

How to improve Asp.net Mvc Application when passing a large chunk of Json object needed?

asp.net json asp.net-mvc jsonserializer

I'm currently developing an reporting application to compare multiple tests. I need to pass a json object from controller to client side to draw all these charts for the report. I attach here the i...