How do I set Identity seed on an ID column using Entity Framework 4 code first with SQL Compact 4?

痴心易碎 提交于 2019-12-18 09:15:33

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!