2013-07-24 42 views
1

使用Orchard 1.6我已將 public virtual int? BusinessCategory_Id { get; set; }添加到我的'CustomerPartRecord'表中。 並添加Orchard 1.6不允許我創建FK

public int? BusinessCategory_Id 
    { 
     get { return Record.BusinessCategory_Id; } 
     set { Record.BusinessCategory_Id = value; } 
    } 

到我的'CustomerPart'表。在我的移民文件我還接着說:

public int UpdateFrom26() 
    { 
     SchemaBuilder.CreateForeignKey("ProductCategoryRecord_CustomerPartRecord", "CustomerPartRecord", new[] { "BusinessCategory_Id" }, "ProductCategoryRecord", new[] { "Id" }); 
     return 27; 
    } 

所以這應該從「ProductCategoryRecord」表走「ID」,並把它作爲一個在「CustomerPartRecord」表稱爲「BusinessCategory_Id」 FK。

我與其他值之前,嘗試這樣做,它的工作,但這個時候,我正在錯誤:Foreign key 'Umacs_ProductCategoryRecord_CustomerPartRecord' references invalid column 'BusinessCategory_Id' in referencing table 'Umacs_UMACS_Bradmount_CustomerPartRecord'. Could not create constraint. See previous errors.

在日誌文件,它告訴我

2013-07-24 17:38:49,716 [28] Orchard.Data.Migration.DataMigrationManager - An unexpected error orccured while applying migration on UMACS.Bradmount from version 26 
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Data.SqlClient.SqlException: Foreign key 'Umacs_ProductCategoryRecord_CustomerPartRecord' references invalid column 'BusinessCategory_Id' in referencing table 'Umacs_UMACS_Bradmount_CustomerPartRecord'. 
Could not create constraint. See previous errors. 

還試圖刪除緩存文件並重新啓動devl。服務器。

有什麼想法?

+0

您可以在CustomerPartRecord表上創建BusinessCategory_Id列的位置顯示遷移代碼嗎? –

+0

沒有意識到你必須爲你的FK創建一個列。認爲它會使用'SchemaBuilder.CreateForeignKey'時自動完成它,但感謝解決問題的答案 – John

+0

然後我將它作爲答案複製,以便您可以授予它。 –

回答

2

您需要先創建列,然後才能將其設置爲外鍵。

+0

好男人再次歡呼貝特朗 – John