I am transitioning to the Entity Framework from an existing model and database. There are several tables in this database with GUID columns that are not primary keys (or keys at all!). Each table has an identifier column. The GUID columns set the ROWGUIDCOL property, as well as DEFAULT (newid ()).
Now, when I insert into the database, I get all the zeros for this column.
I tried using data annotations:
[DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Guid {get; set;}
The problem is that it removes the identity property in my ID column (and gives me insertion errors). In addition, I noticed that for the next migration, EF actually generates identical SQL for both the up and down methods:
public override void Up() { AlterColumn("dbo.Client", "Guid", c => c.Guid(nullable: false, identity: true)); } public override void Down() { AlterColumn("dbo.Client", "Guid", c => c.Guid(nullable: false)); }
Generated sql:
ALTER TABLE [dbo].[Client] ALTER COLUMN [Guid] [uniqueidentifier] NOT NULL
Why does the above migration create the same sql for both statements? How to get EF to generate GUID? Can I / should I do this in the client (code) space? If I must, how can I guarantee uniqueness between tables?
source share