2014-05-21 62 views
0

的一部分,我有以下的EF代碼第一個實體及其相應的配置:使用自動生成的唯一標識符不屬於主鍵

public class Category 
{ 
    public virtual long Id { get; set; } 
    public Guid Guid { get; set; } 
    public string Name { get; set; } 
} 

public class CategoryConfiguration: 
    EntityTypeConfiguration<Category> 
{ 
    public CategoryConfiguration() 
    { 
     this.HasKey(entity => entity.Id); 
     this.Property(entity => entity.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 
     this.Property(entity => entity.Guid).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 
    } 
} 

設置DatabaseGeneratedOption.Identity選項場上Guid似乎有沒有效果,並且SQL Server中的相應表格禁用了IDENTITY選項。

有沒有辦法在EF中自動生成一個GUID/UNIQUEIDENTIFIER列,但同時不是主鍵的一部分?

+0

'IDENTITY'只適用於數字數據類型('int','逗號後有0位數的小數')。它**不能**被應用於'uniqueidentifier' –

+0

可能的重複[實體框架代碼首先使用Guid作爲與另一個標識列的標識](http://stackoverflow.com/questions/12012736/entity-framework-code-first -using-guid-as-identity-with-another-identity-column) – Colin

+0

難道你不能在實體的構造函數中做'Guid.NewGuid()'嗎?當你創建一個全新的,它將把guid設置爲新的東西。當你加載一個現有的實體時,它會將該值設置爲數據庫中的任何值。 – Dismissile

回答

1

EF不會「自動生成」與ID相關的任何內容,這些都是由數據庫處理的。另外DatabaseGeneratedOption.Identity不與主鍵綁定,它只是EF的一個指示器,該字段值由DB生成,因此應拉下。

只要您的字段的數據庫默認值設置爲newid()newsequentialid()那麼DatabaseGeneratedOption.Identity應該工作。

0

一個黑客...

public calss Guid1 { 
    public Guid Guid { get; set; } 
} 

public class Category { 
    public virtual long Id { get; set; } 
    public Guid1 Guid { get; set; } 
    public string Name { get; set; } 
} 

public class CategoryConfiguration:EntityTypeConfiguration<Category> { 
    public CategoryConfiguration() { 
     HasKey(entity => entity.Id); 
     Property(x => x.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 
     HasReqired(x => x.Guid1); 
    } 
} 

public class Guid1Configuration:EntityTypeConfiguration<Guid1> { 
    public Guid1Configuration() { 
     HasKey(x => x.Guid); 
     Property(x => x.Guid).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 
    } 
} 

這應該工作,但你會得到一個無用的表GUID1。

0

對於使用EntityFramworkCore(.NET核心)搜索的好處,你可以做到以下幾點:

EF模型(上創建通知設置默認值)

public class Blog 
    { 
     public int BlogId { get; set; } 
     public string Url { get; set; } 

     public Guid BlogGuid { get; set; } = Guid.NewGuid(); 

     public List<Post> Posts { get; set; } 
    } 

創建遷移,然後改變默認爲defaultValueSql:「newid()」:

public partial class BlogGuidTest : Migration 
    { 
     protected override void Up(MigrationBuilder migrationBuilder) 
     { 
      migrationBuilder.AddColumn<Guid>(
       name: "BlogGuid", 
       table: "Blogs", 
       nullable: false, 
       defaultValueSql: "newid()"); 

      // Add contraint to be safe 
      migrationBuilder.AddUniqueConstraint("BlogGuid_Unique", "Blogs", "BlogGuid"); 
     } 

     protected override void Down(MigrationBuilder migrationBuilder) 
     { 
      migrationBuilder.DropUniqueConstraint("BlogGuid_Unique", "Blogs"); 

      migrationBuilder.DropColumn(
       name: "BlogGuid", 
       table: "Blogs"); 
     } 
    } 

然後更新數據庫。

似乎爲我工作好。