Orchard 1.6 Wont allow me to create a FK

Topics: Troubleshooting
Jul 24, 2013 at 4:43 PM
Using Orchard 1.6 I have added public virtual int? BusinessCategory_Id { get; set; } to my 'CustomerPartRecord' table. And added

public int? BusinessCategory_Id
{
    get { return Record.BusinessCategory_Id; }
    set { Record.BusinessCategory_Id = value; }
}
to my 'CustomerPart' table. In my Migration file I have then added:

public int UpdateFrom26()
{
    SchemaBuilder.CreateForeignKey("ProductCategoryRecord_CustomerPartRecord", "CustomerPartRecord", new[] { "BusinessCategory_Id" }, "ProductCategoryRecord", new[] { "Id" });
    return 27;
}
So this should take the 'Id' from 'ProductCategoryRecord' table and storing it as a FK called 'BusinessCategory_Id' in the 'CustomerPartRecord' table.

I have tried this before with other values and it has worked but this time I am getting the error: Foreign key 'Umacs_ProductCategoryRecord_CustomerPartRecord' references invalid column 'BusinessCategory_Id' in referencing table 'Umacs_UMACS_Bradmount_CustomerPartRecord'. Could not create constraint. See previous errors.

In the logs file it is telling me
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.
Also tried deleting the cache file and restarting the devl. server.

any ideas?
Coordinator
Jul 25, 2013 at 6:28 AM
I answered on your cross-posted StackOverflow post. Please avoid cross-posting, or at least provide links from one to the other, so you don't waste anybody's time.
Jul 25, 2013 at 12:26 PM
ok thanks bertrand....I thought by cross posting I could offer my question to a wider audience but thanks for the reply and thanks for the answer
Coordinator
Jul 25, 2013 at 6:35 PM
Yes, well, you're also creating more work for people answering unfortunately. Most of us who are providing most answers watch both places. Cross-posting is OK if you provide links from one post to the other.