2017-08-16 30 views
1

我有一個類「BudgetDetail」是這樣的:標識列不應用於數據庫

public class BudgetDetail 
{ 
    public int Id { get; set; } 
    public Budget Budget{ get; set; } 
    public int BudgetId { get; set; } 
    public Product Product { get; set; } 
    public int ProductId { get; set; } 
    public byte Quantity { get; set; } 
    public int Price { get; set; } 
    public int Iva { get; set; } 
    public int Total { get; set; } 
} 

這是此模型的流暢API配置:

public class BudgetDetailConfiguration: EntityTypeConfiguration<BudgetDetail> 
{ 
    public BudgetDetailConfiguration() 
    { 
     ToTable("BudgetDetails"); 

     HasKey(pd => new { pd.Id, pd.BudgetId, pd.ProductId }); 

     Property(pd => pd.Id) 
      .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 
    } 
} 

當我做遷移,Id屬性的身份設置爲true,但如果我在數據庫中查看它的身份設置爲false,並且我不知道爲什麼,我猜這是因爲我對此表有複合鍵。

如果您有組合鍵,標識列不起作用?

+2

我不知道你的問題的答案,但包括身份的複合密鑰沒有意義。你只需要標識列就可以是唯一的。添加其他列是多餘的。 –

回答

1

您有一個BudgetId和一個預算 - 與產品相同。添加兩個並不意味着它們是相關的。 Budget對象與BudgetId無關 - BudgetDetails類有兩個不同的屬性 - 一個是BudgetId(FK),另一個是實際的Budget對象。

移除你的對象並保留它們的PK - 它們是BudgetDetail類中的FK。

public class BudgetDetail 
{ 
    public int Id { get; set; } 
    // public Budget Budget{ get; set; } 
    public int BudgetId { get; set; } 
    // public Product Product { get; set; } 
    public int ProductId { get; set; } 
    .../... 
} 
相關問題