context.database.create() 抛出异常无效列名"Branch_Id"
context.database.create() throws exception invalid column name "Branch_Id"
我有一个添加了一次并成功应用的迁移:
public partial class AddedTablesForBranchData : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.SalesArea",
c => new
{
PostalCode = c.Int(nullable: false, identity: true),
Location = c.String(),
Branch_Id = c.String(maxLength: 128),
})
.PrimaryKey(t => t.PostalCode)
.ForeignKey("dbo.SalesBranch", t => t.Branch_Id)
.Index(t => t.Branch_Id);
CreateTable(
"dbo.SalesBranch",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Name = c.String(),
Contacts = c.String(),
})
.PrimaryKey(t => t.Id);
}
public override void Down()
{
DropForeignKey("dbo.SalesArea", "Branch_Id", "dbo.SalesBranch");
DropIndex("dbo.SalesArea", new[] { "Branch_Id" });
DropTable("dbo.SalesBranch");
DropTable("dbo.SalesArea");
}
}
遗憾的是 PostalCode 是一个整数。由于本地化,我不得不将其更改为字符串...
因此一些迁移后来我添加了一个新的迁移:
public partial class ReCreateSalesTables : DbMigration
{
public override void Up()
{
DropForeignKey("SalesArea", "Branch_Id", "SalesBranch");
DropIndex("SalesArea", new[] { "Branch_Id" });
DropTable("dbo.SalesArea");
DropTable("dbo.SalesBranch");
CreateTable("SalesBranch",
c => new
{
Id = c.String(false, maxLength: 128),
Name = c.String(),
Contacts = c.String()
})
.PrimaryKey(t => t.Id);
CreateTable("SalesArea",
c => new
{
Id = c.Int(false, true),
PostalCode = c.String(maxLength: 32),
Location = c.String(),
BranchId = c.String(nullable: false, maxLength: 128)
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.SalesBranch", t => t.BranchId)
.Index(t => t.PostalCode, unique: true);
}
public override void Down()
{
throw new Exception("It has never been our intention to use a down migration, else data might be lost...");
}
}
然后我 运行 进入“a table con not have multiple identity columns”问题由于 PostalCode 是一个标识列,在新迁移中我有 Id 是一个标识列 Identity
因此,我不得不在新迁移中删除两个 table,并使用新架构重新创建这些 table。
我的本地 machine/development 环境似乎没有问题但是当我 运行 集成测试或任何测试之前 运行 我这样做:
[TestClass]
public sealed class InitializeDatabase
{
[AssemblyInitialize]
public static void AssemblyInit(TestContext x)
{
using (var context = new LeadContext())
{
// Create database outside of the test transactions else you get a nice exception...
context.Database.Delete();
context.Database.Create();
new Configuration().FillEnums(context);
}
}
}
正在删除旧数据库并使用所有迁移创建新数据库。这个 AssemblyInit 方法 运行 在我调试时很好,但在几秒钟后离开该方法后,我可以在我的集成测试中看到这个输出:
结果消息:初始化方法 IntegrationTests.SalesDataTests.Init 抛出异常。 System.Data.Entity.Core.EntityCommandExecutionException: System.Data.Entity.Core.EntityCommandExecutionException: 执行命令定义时发生错误。有关详细信息,请参阅内部异常。 ---> System.Data.SqlClient.SqlException: 无效的列名 'Branch_Id'..
我无法直接调试我的集成测试,因为我从未到达那里,所以问题一定是 context.database.create() 方法。
为什么 EF 抱怨 old/former 外键列 'Branch_Id' 无效?
我不明白那个场景。
有人可以帮忙吗:-)
更新
问题:
AddedTablesForBranchData 和迁移 ReCreateSalesTables 之间发生了什么变化?
回答:
我引入了一个 属性 Id (identity column/string) 并将 属性 PostalCode 更改为 integer/unique:true.
更新 2
不存在关于任何 SalesX 的流畅配置 table。
型号
[Table("SalesBranch")]
public class SalesBranch
{
[Key]
public string Id { get; set; }
public string Name { get; set; }
public string Contacts { get; set; }
public virtual ICollection<SalesArea> SalesAreas { get; set; }
}
[Table("SalesArea")]
public class SalesArea
{
public int Id { get; set; }
public string PostalCode { get; set; }
public string Location { get; set; }
public virtual SalesBranch Branch { get; set; }
public int BranchId { get; set; }
}
通过注释 属性:
public virtual SalesBranch Branch { get; set; }
和 [ForeignKey("BranchId")]
解决了问题!
模型似乎与迁移不同步!
我有一个添加了一次并成功应用的迁移:
public partial class AddedTablesForBranchData : DbMigration
{
public override void Up()
{
CreateTable(
"dbo.SalesArea",
c => new
{
PostalCode = c.Int(nullable: false, identity: true),
Location = c.String(),
Branch_Id = c.String(maxLength: 128),
})
.PrimaryKey(t => t.PostalCode)
.ForeignKey("dbo.SalesBranch", t => t.Branch_Id)
.Index(t => t.Branch_Id);
CreateTable(
"dbo.SalesBranch",
c => new
{
Id = c.String(nullable: false, maxLength: 128),
Name = c.String(),
Contacts = c.String(),
})
.PrimaryKey(t => t.Id);
}
public override void Down()
{
DropForeignKey("dbo.SalesArea", "Branch_Id", "dbo.SalesBranch");
DropIndex("dbo.SalesArea", new[] { "Branch_Id" });
DropTable("dbo.SalesBranch");
DropTable("dbo.SalesArea");
}
}
遗憾的是 PostalCode 是一个整数。由于本地化,我不得不将其更改为字符串...
因此一些迁移后来我添加了一个新的迁移:
public partial class ReCreateSalesTables : DbMigration
{
public override void Up()
{
DropForeignKey("SalesArea", "Branch_Id", "SalesBranch");
DropIndex("SalesArea", new[] { "Branch_Id" });
DropTable("dbo.SalesArea");
DropTable("dbo.SalesBranch");
CreateTable("SalesBranch",
c => new
{
Id = c.String(false, maxLength: 128),
Name = c.String(),
Contacts = c.String()
})
.PrimaryKey(t => t.Id);
CreateTable("SalesArea",
c => new
{
Id = c.Int(false, true),
PostalCode = c.String(maxLength: 32),
Location = c.String(),
BranchId = c.String(nullable: false, maxLength: 128)
})
.PrimaryKey(t => t.Id)
.ForeignKey("dbo.SalesBranch", t => t.BranchId)
.Index(t => t.PostalCode, unique: true);
}
public override void Down()
{
throw new Exception("It has never been our intention to use a down migration, else data might be lost...");
}
}
然后我 运行 进入“a table con not have multiple identity columns”问题由于 PostalCode 是一个标识列,在新迁移中我有 Id 是一个标识列 Identity
因此,我不得不在新迁移中删除两个 table,并使用新架构重新创建这些 table。
我的本地 machine/development 环境似乎没有问题但是当我 运行 集成测试或任何测试之前 运行 我这样做:
[TestClass]
public sealed class InitializeDatabase
{
[AssemblyInitialize]
public static void AssemblyInit(TestContext x)
{
using (var context = new LeadContext())
{
// Create database outside of the test transactions else you get a nice exception...
context.Database.Delete();
context.Database.Create();
new Configuration().FillEnums(context);
}
}
}
正在删除旧数据库并使用所有迁移创建新数据库。这个 AssemblyInit 方法 运行 在我调试时很好,但在几秒钟后离开该方法后,我可以在我的集成测试中看到这个输出:
结果消息:初始化方法 IntegrationTests.SalesDataTests.Init 抛出异常。 System.Data.Entity.Core.EntityCommandExecutionException: System.Data.Entity.Core.EntityCommandExecutionException: 执行命令定义时发生错误。有关详细信息,请参阅内部异常。 ---> System.Data.SqlClient.SqlException: 无效的列名 'Branch_Id'..
我无法直接调试我的集成测试,因为我从未到达那里,所以问题一定是 context.database.create() 方法。
为什么 EF 抱怨 old/former 外键列 'Branch_Id' 无效?
我不明白那个场景。
有人可以帮忙吗:-)
更新
问题: AddedTablesForBranchData 和迁移 ReCreateSalesTables 之间发生了什么变化?
回答: 我引入了一个 属性 Id (identity column/string) 并将 属性 PostalCode 更改为 integer/unique:true.
更新 2
不存在关于任何 SalesX 的流畅配置 table。
型号
[Table("SalesBranch")]
public class SalesBranch
{
[Key]
public string Id { get; set; }
public string Name { get; set; }
public string Contacts { get; set; }
public virtual ICollection<SalesArea> SalesAreas { get; set; }
}
[Table("SalesArea")]
public class SalesArea
{
public int Id { get; set; }
public string PostalCode { get; set; }
public string Location { get; set; }
public virtual SalesBranch Branch { get; set; }
public int BranchId { get; set; }
}
通过注释 属性:
public virtual SalesBranch Branch { get; set; }
和 [ForeignKey("BranchId")]
解决了问题!
模型似乎与迁移不同步!