2015-08-14 45 views
1

我創建了實體框架代碼優先模型,並有兩個類如何插入表中的數據時,外鍵是不是主鍵

Product.cs

public class Product 
    { 
     public Product() 
     { 
      Certificates = new HashSet<Certificate>(); 
     } 

     public int ProductId { get; set; } 
     public int ProductCode { get; set; } 
     public decimal Price { get; set; } 
     public string Type { get; set; } 
     public string SubType { get; set; } 
     public int RelatedProductId { get; set; } 
     public string Description { get; set; } 
     public string DescriptionSpanish { get; set; } 
     public string ShortDescription { get; set; } 
     public string CertificateDescription { get; set; } 
     public string QBItemCode { get; set; } 
     public string QBDescCode { get; set; } 
     public virtual ICollection<Certificate> Certificates { get; set; } 
    } 

Certificate.cs

public class Certificate 
    { 
     public int CertificateId { get; set; } 
     public string Type { get; set; } 
     public string Course { get; set; } 
     public DateTime DateGranted { get; set; } 
     public string NameOnCertificate { get; set; } 
     public int FinalExamQuestionsCorrect { get; set; } 
     public int FinalExamQuestionsTotal { get; set; } 
     public string ClientIPAddress { get; set; } 
     public int ProductCode { get; set; } 
     public virtual Product Product { get; set; } 
     public string UserId { get; set; } 
     public virtual ApplicationUser ApplicationUser { get; set; } 
    } 

ProductConfiguration.cs

public class ProductConfiguration : EntityTypeConfiguration<Product> 
    { 
     public ProductConfiguration() 
     { 
      HasKey(p => p.ProductId); 

      Property(p => p.ProductId) 
       .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 


      Property(p => p.ProductCode) 
       .HasColumnAnnotation("Index", 
        new IndexAnnotation(new IndexAttribute("AK_Product_ProductCode") { IsUnique = true })); 


     } 
    } 

CertificateConfiguration.cs

public class CertificateConfiguration : EntityTypeConfiguration<Certificate> 
    { 
     public CertificateConfiguration() 
     { 
      HasKey(c => c.CertificateId); 

      Property(c => c.CertificateId) 
       .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity); 



      Property(p => p.ProductCode).HasColumnName("ProductId"); 


      HasRequired(c => c.ApplicationUser) 
       .WithMany(u => u.Certificates) 
       .HasForeignKey(c => c.UserId); 

      HasRequired(c => c.Product) 
       .WithMany(p => p.Certificates) 
       .HasForeignKey(c => c.ProductCode); 
     } 
    } 

通告,我創建了一個Foreign KeyProductCodeCertificate Table(其重命名爲ProductId)。

我還接種Producttable與其中通過database和我手動定義ProductCode產生的ProductId一些數據。問題是,當我試圖通過運行在我定義了一個foreign key這實際上是一個sql查詢插入一條記錄在Certificatetable一個ProductCode(不ProductId),它拋出一個錯誤

SQL查詢:

Insert into [dbo].[Certificates] 
values (
'TestType','TestCourse',GETUTCDATE(),'TestName',1,5, 
'127.0.0.1',201,'userId' 
) 

201處於查詢存在於產品表產品代碼

錯誤

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_dbo.Certificates_dbo.Products_ProductId". The conflict occurred in database "myDatabse", table "dbo.Products", column 'ProductId'. 

我不知道爲什麼它尋找主鍵ProductId。它應該在Product表中查找ProductCode它存在或不存在。

+0

你說你試圖用SQL查詢插入證書表...你可以發佈一個樣本嗎? – Peter

+0

請參閱更新 –

回答

0

我相信你會遇到EF的限制,因此你的FK在關係的依賴端必須返回到主體端的PK。當您配置ProductCertificates這裏的關係:

HasRequired(c => c.Product) 
    .WithMany(p => p.Certificates) 
    .HasForeignKey(c => c.ProductCode); 

此建立了產品主鍵的外鍵關係 - 你所定義的外鍵 - - Certificate.ProductCode(命名爲您的表Certificate.ProductIdProduct.ProductId

儘管數據庫將支持FK引用表中的唯一鍵,但EF不支持(此時)。

下面是涉及這一問題的幾個其他SO問題:

How to get EF6 to honor Unique Constraint (on FK) in Association/Relationship multiplicity?

Database first Entity Framework mapping unique foreign keys as one to many

下面是官方的功能要求將此功能添加到EF:

Unique Constraint (i.e. Candidate Key) Support

根據評論,它看起來像他們正在添加這在EF7中。

+0

所以,沒有辦法建立'Product.ProductCode'和'Certificate.ProductCode'之間的關係(名爲'Certificate.ProductId'? –

+0

不是我所知道的,也許你能夠通過將Product.ProductCode作爲主鍵來僞裝它,但這可能會對您可能需要定義的其他關係產生影響。 – Peter