Reputation: 888
I'm trying to update my database with a migration. I probably messed up when I deleted by hand all my tables and then recreated by hand.
I'm using code first, and the Add-Migration command gave me this :
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]");
}
I surrounded every name with the brackets ('[' and ']') which apparently helped some people in my case. I'm posting because, you guessed it, it did not for me.
I'm always getting the :
Either the parameter @objname is ambiguous or the claimed @objtype (COLUMN) is wrong.
Would anyone have a guess about what is forbidding me from updating my database ? Should I start fresh from another project ?
Thanks !
EDIT : After comment poiting my brackets were placed in the wrong way, I updated to :
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]");
}
}
Which does not solve my problem unfortunately.
Upvotes: 1
Views: 9414
Reputation: 187
My problem was that for some reason, when I ran Create-Migration
, it generated a command referencing a field in a table that didn't exist. Once I fixed that, it ran perfectly.
Upvotes: 2