Search code examples
c#entity-frameworkforeign-keysdatabase-migration

Entity Framework 6 could not create a foreign key


I'm writing an application that use the migration engine of EF 6 to handle a database creation. Using the CSharpMigrationCodeGenerator class, I build a class library assembly within a migration that will be processed by the DbMigrator. It works fine but with the foreign key I get this error:

Exception Details: System.Data.Entity.Migrations.Infrastructure.MigrationsException: The Foreign Key on table 'dbo.Tabellina' with columns 'Tabella_Id' could not be created because the principal key columns could not be determined. Use the AddForeignKey fluent API to fully specify the Foreign Key.

The migration code is this:

namespace AppMig.Migrations
{
    using System;
    using System.Data.Entity.Migrations;

    public partial class CreateTables: DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Tabella",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        FromDate = c.DateTime(),
                        Todate = c.DateTime(),
                        campotabella = c.String(maxLength: 50),
                    })
                .Index(t => t.FromDate, name: "tabella_fromdate_index")
                .Index(t => t.Todate, name: "tabella_todate_index");

            AddPrimaryKey("dbo.Tabella", "Id", name: "tabella_id_index");
            CreateTable(
                "dbo.Tabellina",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        FromDate = c.DateTime(),
                        Todate = c.DateTime(),
                        campotabellina = c.String(maxLength: 50),
                        Tabella_Id = c.Int(nullable: false),
                    })
                .ForeignKey("dbo.Tabella", t => t.Tabella_Id, cascadeDelete: true)
                .Index(t => t.FromDate, name: "tabellina_fromdate_index")
                .Index(t => t.Todate, name: "tabellina_todate_index")
                .Index(t => t.Tabella_Id);

            AddPrimaryKey("dbo.Tabellina", "Id", name: "tabellina_id_index");
        }

        public override void Down()
        {
            DropIndex("dbo.Tabellina", new[] { "Tabella_Id" });
            DropForeignKey("dbo.Tabellina", "Tabella_Id", "dbo.Tabella");
            DropIndex("dbo.Tabellina", "tabellina_todate_index");
            DropIndex("dbo.Tabellina", "tabellina_fromdate_index");
            DropPrimaryKey("dbo.Tabellina", name: "tabellina_id_index");
            DropTable("dbo.Tabellina");
            DropIndex("dbo.Tabella", "tabella_todate_index");
            DropIndex("dbo.Tabella", "tabella_fromdate_index");
            DropPrimaryKey("dbo.Tabella", name: "tabella_id_index");
            DropTable("dbo.Tabella");
        }
    }
}

This is not working but, if I create a second migration that just add the foreign key (when the two tables are already created), it works. Due to the application structure, is really difficult for me create a second migration so i need to solve this mystery.


Solution

  • Taken from the vanilla instructions, is there a reason why you can't use the .PrimaryKey(t => t.Id) as part of the main CreateTable method?