问题
I am having some problem setting the Identity Seed on the Id column in SQL Compact 4 using the code first approach.
I have tried this
context.Database.ExecuteSqlCommand("DBCC CHECKIDENT ('Members', RESEED, 100001");
but this is not working in Sql Compact.
MyDbContext:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
SetupMemberEntity(modelBuilder);
}
private static void SetupMemberEntity(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Member>().Property(m => m.Id);
//.Property(m => m.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
modelBuilder.Entity<Member>().Property(m => m.FirstName).IsRequired();
modelBuilder.Entity<Member>().Property(m => m.LastName).IsRequired();
modelBuilder.Entity<Member>().Property(m => m.PinCode).IsRequired();
modelBuilder.Entity<Member>().Property(m => m.Email);
//modelBuilder.Entity<Member>().Property(m => m.DateCreated).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
//modelBuilder.Entity<Member>().Property(m => m.DateModified).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
}
I have added one more property MemberId to the Member entity and have tried to use
context.Database.ExecuteSqlCommand("ALTER TABLE Members ADD MemberId INT IDENTITY(10000,1) PRIMARY KEY");
but I get error message that a table can only contain one identity, but I havent set any IDENTITY so, is the Id column auto IDENTITY ?
回答1:
I found the answer here:
context.Database
.ExecuteSqlCommand("ALTER TABLE Members ALTER COLUMN Id IDENTITY (10000,1)");
来源:https://stackoverflow.com/questions/11732102/how-do-i-set-identity-seed-on-an-id-column-using-entity-framework-4-code-first-w