I assume that you are using SQL Server as your database. This is a good example of inconsistency between different MS tools. The SQL server command does not recommend using newid() as the default value for UNIQUEIDENTIFIER columns, and the ADO.NET command uses it if you specify the Guid property as auto-generated in the database. Instead, they should use newsequentialid() !
If you want the sequential Guides generated by the database, you have to modify the generated table, and this is really difficult, because you have to find the default auto-generated constraint, delete it and create a new constraint. All of this can be done in a custom database initializer. Here you have a sample code:
class Program { static void Main(string[] args) { Database.SetInitializer(new CustomInitializer()); using (var context = new Context()) { context.TestEntities.Add(new TestEntity() { Name = "A" }); context.TestEntities.Add(new TestEntity() { Name = "B" }); context.SaveChanges(); } } } public class CustomInitializer : DropCreateDatabaseAlways<Context> { protected override void Seed(Context context) { base.Seed(context); context.Database.ExecuteSqlCommand(@" DECLARE @Name VARCHAR(100) SELECT @Name = O.Name FROM sys.objects AS O INNER JOIN sys.tables AS T ON O.parent_object_id = T.object_id WHERE O.type_desc LIKE 'DEFAULT_CONSTRAINT' AND O.Name LIKE 'DF__TestEntities__Id__%' AND T.Name = 'TestEntities' DECLARE @Sql NVARCHAR(2000) = 'ALTER TABLE TestEntities DROP Constraint ' + @Name EXEC sp_executesql @Sql ALTER TABLE TestEntities ADD CONSTRAINT IdDef DEFAULT NEWSEQUENTIALID() FOR Id"); } } public class TestEntity { public Guid Id { get; set; } public string Name { get; set; } } public class Context : DbContext { public DbSet<TestEntity> TestEntities { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.Entity<TestEntity>() .Property(e => e.Id) .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); } }
Ladislav Mrnka May 23 '11 at 17:20 2011-05-23 17:20
source share