C# EF迁移:参数@objname不明确或声明的@objtype(列)错误
我正在尝试使用迁移更新我的数据库。 当我手工删除所有表格,然后手工重新创建时,我可能把事情搞砸了 我首先使用代码,Add Migration命令告诉我:C# EF迁移:参数@objname不明确或声明的@objtype(列)错误,c#,entity-framework,ef-code-first,entity-framework-migrations,C#,Entity Framework,Ef Code First,Entity Framework Migrations,我正在尝试使用迁移更新我的数据库。 当我手工删除所有表格,然后手工重新创建时,我可能把事情搞砸了 我首先使用代码,Add Migration命令告诉我: public override void Up() { DropForeignKey("[dbo.Products]", "[Bundle_Id]", "[dbo.Bundles]"); DropForeignKey("[dbo.Defects]", "[Product_Id]", "[dbo.
public override void Up()
{
DropForeignKey("[dbo.Products]", "[Bundle_Id]", "[dbo.Bundles]");
DropForeignKey("[dbo.Defects]", "[Product_Id]", "[dbo.Products]");
DropForeignKey("[dbo.Defects]", "[User_Id]", "[dbo.Users]");
RenameColumn(table: "[dbo.Products]", name: "[Bundle_Id]", newName: "[Bundles_BundlesId]");
RenameColumn(table: "[dbo.Defects]", name: "[Product_Id]", newName: "[Products_ProductsId]");
RenameColumn(table: "[dbo.Defects]", name: "[User_Id]", newName: "[Users_UsersId]");
RenameIndex(table: "[dbo.Products]", name: "[IX_Bundle_Id]", newName: "[IX_Bundles_BundlesId]");
RenameIndex(table: "[dbo.Defects]", name: "[IX_Product_Id]", newName: "[IX_Products_ProductsId]");
RenameIndex(table: "[dbo.Defects]", name: "[IX_User_Id]", newName: "[IX_Users_UsersId]");
DropPrimaryKey("[dbo.Bundles]");
DropPrimaryKey("[dbo.Defects]");
DropPrimaryKey("[dbo.Products]");
DropPrimaryKey("[dbo.Users]");
AddColumn("[dbo.Bundles]", "[BundlesId]", c => c.Long(nullable: false, identity: true));
AddColumn("[dbo.Defects]", "[DefectsId]", c => c.Long(nullable: false, identity: true));
AddColumn("[dbo.Defects]", "[ProductsId]", c => c.Int(nullable: false));
AddColumn("[dbo.Defects]", "[UsersId]", c => c.Int(nullable: false));
AddColumn("[dbo.Products]", "[ProductsId]", c => c.Long(nullable: false, identity: true));
AddColumn("[dbo.Products]", "[BundlesId]", c => c.Int(nullable: false));
AddColumn("[dbo.Users]", "[UsersId]", c => c.Long(nullable: false, identity: true));
AddPrimaryKey("[dbo.Bundles]", "[BundlesId]");
AddPrimaryKey("[dbo.Defects]", "[DefectsId]");
AddPrimaryKey("[dbo.Products]", "[ProductsId]");
AddPrimaryKey("[dbo.Users]", "[UsersId]");
AddForeignKey("[dbo.Products]", "[Bundles_BundlesId]", "[dbo.Bundles]", "[BundlesId]");
AddForeignKey("[dbo.Defects]", "[Products_ProductsId]", "[dbo.Products]", "[ProductsId]");
AddForeignKey("[dbo.Defects]", "[Users_UsersId]", "[dbo.Users]", "[UsersId]");
DropColumn("[dbo.Bundles]", "[Id]");
DropColumn("[dbo.Defects]", "[Id]");
DropColumn("[dbo.Defects]", "[ProductId]");
DropColumn("[dbo.Defects]", "[UserId]");
DropColumn("[dbo.Products]", "[Id]");
DropColumn("[dbo.Products]", "[BundleId]");
DropColumn("[dbo.Users]", "[Id]");
}
public override void Down()
{
AddColumn("[dbo.Users]", "[Id]", c => c.Long(nullable: false, identity: true));
AddColumn("[dbo.Products]", "[BundleId]", c => c.Int(nullable: false));
AddColumn("[dbo.Products]", "[Id]", c => c.Long(nullable: false, identity: true));
AddColumn("[dbo.Defects]", "[UserId]", c => c.Int(nullable: false));
AddColumn("[dbo.Defects]", "[ProductId]", c => c.Int(nullable: false));
AddColumn("[dbo.Defects]", "[Id]", c => c.Long(nullable: false, identity: true));
AddColumn("[dbo.Bundles]", "[Id]", c => c.Long(nullable: false, identity: true));
DropForeignKey("[dbo.Defects]", "[Users_UsersId]", "[dbo.Users]");
DropForeignKey("[dbo.Defects]", "[Products_ProductsId]", "[dbo.Products]");
DropForeignKey("[dbo.Products]", "[Bundles_BundlesId]", "[dbo.Bundles]");
DropPrimaryKey("[dbo.Users]");
DropPrimaryKey("[dbo.Products]");
DropPrimaryKey("[dbo.Defects]");
DropPrimaryKey("[dbo.Bundles]");
DropColumn("[dbo.Users]", "[UsersId]");
DropColumn("[dbo.Products]", "[BundlesId]");
DropColumn("[dbo.Products]", "[ProductsId]");
DropColumn("[dbo.Defects]", "[UsersId]");
DropColumn("[dbo.Defects]", "[ProductsId]");
DropColumn("[dbo.Defects]", "[DefectsId]");
DropColumn("[dbo.Bundles]", "[BundlesId]");
AddPrimaryKey("[dbo.Users]", "[Id]");
AddPrimaryKey("[dbo.Products]", "[Id]");
AddPrimaryKey("[dbo.Defects]", "[Id]");
AddPrimaryKey("[dbo.Bundles]", "[Id]");
RenameIndex(table: "[dbo.Defects]", name: "[IX_Users_UsersId]", newName: "[IX_User_Id]");
RenameIndex(table: "[dbo.Defects]", name: "[IX_Products_ProductsId]", newName: "[IX_Product_Id]");
RenameIndex(table: "[dbo.Products]", name: "[IX_Bundles_BundlesId]", newName: "[IX_Bundle_Id]");
RenameColumn(table: "[dbo.Defects]", name: "[Users]_UsersId]", newName: "[User_Id]");
RenameColumn(table: "[dbo.Defects]", name: "[Products_ProductsId]", newName: "[Product_Id]");
RenameColumn(table: "[dbo.Products]", name: "[Bundles_BundlesId]", newName: "[Bundle_Id]");
AddForeignKey("[dbo.Defects]", "[User_Id]", "[dbo.Users]", "[Id]");
AddForeignKey("[dbo.Defects]", "[Product_Id]", "[dbo.Products]", "[Id]");
AddForeignKey("[dbo.Products]", "[Bundle_Id]", "[dbo.Bundles]", "[Id]");
}
我用括号(“[”和“]]”包围了每个名字,这显然对我的案例中的一些人有所帮助。
我发帖是因为,你猜对了,它不适合我
我总是得到:
参数@objname不明确或声明的@objtype(列)错误
谁能猜出是什么阻止我更新我的数据库?我应该从另一个项目重新开始吗
谢谢
编辑:在注释中指出我的括号放置错误后,我更新为:
public partial class Migr : DbMigration
{
public override void Up()
{
DropForeignKey("[dbo].[Products]", "[Bundle_Id]", "[dbo].[Bundles]");
DropForeignKey("[dbo].[Defects]", "[Product_Id]", "[dbo].[Products]");
DropForeignKey("[dbo].[Defects]", "[User_Id]", "[dbo].[Users]");
RenameColumn(table: "[dbo].[Products]", name: "[Bundle_Id]", newName: "[Bundles_BundlesId]");
RenameColumn(table: "[dbo].[Defects]", name: "[Product_Id]", newName: "[Products_ProductsId]");
RenameColumn(table: "[dbo].[Defects]", name: "[User_Id]", newName: "[Users_UsersId]");
RenameIndex(table: "[dbo].[Products]", name: "[IX_Bundle_Id]", newName: "[IX_Bundles_BundlesId]");
RenameIndex(table: "[dbo].[Defects]", name: "[IX_Product_Id]", newName: "[IX_Products_ProductsId]");
RenameIndex(table: "[dbo].[Defects]", name: "[IX_User_Id]", newName: "[IX_Users_UsersId]");
DropPrimaryKey("[dbo].[Bundles]");
DropPrimaryKey("[dbo].[Defects]");
DropPrimaryKey("[dbo].[Products]");
DropPrimaryKey("[dbo].[Users]");
AddColumn("[dbo].[Bundles]", "[BundlesId]", c => c.Long(nullable: false, identity: true));
AddColumn("[dbo].[Defects]", "[DefectsId]", c => c.Long(nullable: false, identity: true));
AddColumn("[dbo].[Defects]", "[ProductsId]", c => c.Int(nullable: false));
AddColumn("[dbo].[Defects]", "[UsersId]", c => c.Int(nullable: false));
AddColumn("[dbo].[Products]", "[ProductsId]", c => c.Long(nullable: false, identity: true));
AddColumn("[dbo].[Products]", "[BundlesId]", c => c.Int(nullable: false));
AddColumn("[dbo].[Users]", "[UsersId]", c => c.Long(nullable: false, identity: true));
AddPrimaryKey("[dbo].[Bundles]", "[BundlesId]");
AddPrimaryKey("[dbo].[Defects]", "[DefectsId]");
AddPrimaryKey("[dbo].[Products]", "[ProductsId]");
AddPrimaryKey("[dbo].[Users]", "[UsersId]");
AddForeignKey("[dbo].[Products]", "[Bundles_BundlesId]", "[dbo].[Bundles]", "[BundlesId]");
AddForeignKey("[dbo].[Defects]", "[Products_ProductsId]", "[dbo].[Products]", "[ProductsId]");
AddForeignKey("[dbo].[Defects]", "[Users_UsersId]", "[dbo].[Users]", "[UsersId]");
DropColumn("[dbo].[Bundles]", "[Id]");
DropColumn("[dbo].[Defects]", "[Id]");
DropColumn("[dbo].[Defects]", "[ProductId]");
DropColumn("[dbo].[Defects]", "[UserId]");
DropColumn("[dbo].[Products]", "[Id]");
DropColumn("[dbo].[Products]", "[BundleId]");
DropColumn("[dbo].[Users]", "[Id]");
}
public override void Down()
{
AddColumn("[dbo].[Users]", "[Id]", c => c.Long(nullable: false, identity: true));
AddColumn("[dbo].[Products]", "[BundleId]", c => c.Int(nullable: false));
AddColumn("[dbo].[Products]", "[Id]", c => c.Long(nullable: false, identity: true));
AddColumn("[dbo].[Defects]", "[UserId]", c => c.Int(nullable: false));
AddColumn("[dbo].[Defects]", "[ProductId]", c => c.Int(nullable: false));
AddColumn("[dbo].[Defects]", "[Id]", c => c.Long(nullable: false, identity: true));
AddColumn("[dbo].[Bundles]", "[Id]", c => c.Long(nullable: false, identity: true));
DropForeignKey("[dbo].[Defects]", "[Users_UsersId]", "[dbo].[Users]");
DropForeignKey("[dbo].[Defects]", "[Products_ProductsId]", "[dbo].[Products]");
DropForeignKey("[dbo].[Products]", "[Bundles_BundlesId]", "[dbo].[Bundles]");
DropPrimaryKey("[dbo].[Users]");
DropPrimaryKey("[dbo].[Products]");
DropPrimaryKey("[dbo].[Defects]");
DropPrimaryKey("[dbo].[Bundles]");
DropColumn("[dbo].[Users]", "[UsersId]");
DropColumn("[dbo].[Products]", "[BundlesId]");
DropColumn("[dbo].[Products]", "[ProductsId]");
DropColumn("[dbo].[Defects]", "[UsersId]");
DropColumn("[dbo].[Defects]", "[ProductsId]");
DropColumn("[dbo].[Defects]", "[DefectsId]");
DropColumn("[dbo].[Bundles]", "[BundlesId]");
AddPrimaryKey("[dbo].[Users]", "[Id]");
AddPrimaryKey("[dbo].[Products]", "[Id]");
AddPrimaryKey("[dbo].[Defects]", "[Id]");
AddPrimaryKey("[dbo].[Bundles]", "[Id]");
RenameIndex(table: "[dbo].[Defects]", name: "[IX_Users_UsersId]", newName: "[IX_User_Id]");
RenameIndex(table: "[dbo].[Defects]", name: "[IX_Products_ProductsId]", newName: "[IX_Product_Id]");
RenameIndex(table: "[dbo].[Products]", name: "[IX_Bundles_BundlesId]", newName: "[IX_Bundle_Id]");
RenameColumn(table: "[dbo].[Defects]", name: "[Users]_UsersId]", newName: "[User_Id]");
RenameColumn(table: "[dbo].[Defects]", name: "[Products_ProductsId]", newName: "[Product_Id]");
RenameColumn(table: "[dbo].[Products]", name: "[Bundles_BundlesId]", newName: "[Bundle_Id]");
AddForeignKey("[dbo].[Defects]", "[User_Id]", "[dbo].[Users]", "[Id]");
AddForeignKey("[dbo].[Defects]", "[Product_Id]", "[dbo].[Products]", "[Id]");
AddForeignKey("[dbo].[Products]", "[Bundle_Id]", "[dbo].[Bundles]", "[Id]");
}
}
不幸的是,这并没有解决我的问题。我的问题是,出于某种原因,当我运行
Create Migration
时,它生成了一个引用表中不存在的字段的命令。一旦我修好了,它就运行得很好。你的括号错了。例如,[dbo.Users]
应该是[dbo].[Users]
哦,这很有道理,我会试试。谢谢没有运气,没有改变我的问题。谢谢你的努力。你想完成什么?如果您只想基于代码优先模型重建数据库,则不需要迁移-您可以使用DropCreate之类的初始值设定项来完成此操作。。。如果需要同步已部署的数据库,可以使用不同的技术。