C# 实体框架6无法创建外键

C# 实体框架6无法创建外键,c#,entity-framework,foreign-keys,database-migration,C#,Entity Framework,Foreign Keys,Database Migration,我正在编写一个应用程序,它使用EF6的迁移引擎来处理数据库创建。使用CSharpMigrationCodeGenerator类,我在将由DbMigrator处理的迁移中构建了一个类库程序集。它工作正常,但使用外键时,我会出现以下错误: Exception Details: System.Data.Entity.Migrations.Infrastructure.MigrationsException: The Foreign Key on table 'dbo.Tabellina' with c

我正在编写一个应用程序,它使用EF6的迁移引擎来处理数据库创建。使用CSharpMigrationCodeGenerator类,我在将由DbMigrator处理的迁移中构建了一个类库程序集。它工作正常,但使用外键时,我会出现以下错误:

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.
迁移代码如下所示:

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");
        }
    }
}

这不起作用,但是,如果我创建了第二个迁移,在已经创建了两个表的情况下只添加外键,那么它就起作用了。由于应用程序结构的原因,创建第二次迁移对我来说非常困难,因此我需要解开这个谜团。

摘自,有什么原因不能将.PrimaryKeyt=>t.Id作为主要CreateTable方法的一部分吗

由于应用程序结构的原因,它被分离是有原因的,但我尝试手动移动主键作为主要部分,结果根本没有改变