I have a class called Offer as follows:
public class Offer
{
public Guid Id { get; set; }
[DatabaseGenerated(DatabaseGeneratedOptio
Just found a solution for this matter. You can simply call a Sql() method in your Up() method.
public override void Up()
{
CreateTable(
"Offers",
c => new
{
OfferNo = c.Int(nullable: false, identity: true),
...
})
.PrimaryKey(t => t.OfferNo);
Sql("DBCC CHECKIDENT ('Offers', RESEED, 100);");
}
According to the comment, "but starting from a seed value that I provide instead of 1", you can use an identity column, and customize your database initialization or migration to set the seed of your identity column.
The T-SQL command to do this is:
DBCC CHECKIDENT ('Offer', RESEED, 123);
Note that the next inserted value is not 123, but 123 + increment (124 if default increment of 1).
You can also use a column with the DatabaseGeneratedOption.Computed
and a sequence as default value for your field (if you're using a recent SQL server version). When you create a sequence, you can specify the initial value and increment:
CREATE SEQUENCE OfferNoSeq
START WITH 1 -- Initial Value
INCREMENT BY 1 -- Increment
An attach this sequence as a default for the OfferNo
column like this:
ALTER TABLE Offer ADD CONSTRAINT OfferNoSeq
DEFAULT (NEXT VALUE FOR OfferNoSeq) FOR OfferNo;
There is no direct way to implement this in Code First. So, for using any of these options, you need to
Seed
methods (look for the implementation of public class MyInitializer
in the linked article)Up()
or Down()
method of your migration, as shown in the linked SO answerPlease, if you use SEQUENCE, please, read this: EF6 does not work with primary key from sequence.