2017-03-17 306 views
0

我試圖通過遷移更新我的數據庫。 當我手工刪除所有表格然後用手重新創建時,我可能會搞砸了。EF遷移:參數@objname不明確或聲明的@objtype(COLUMN)錯誤

我使用代碼首先,和Add-遷移命令給了我這個:

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(列)是錯誤的。

會有人有一個關於什麼是不能更新我的數據庫,禁止我猜?我應該從另一個項目重新開始嗎?

謝謝!

編輯:評論poiting我的支架放置在錯誤的方式後,我更新爲:

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

這並不遺憾的是解決我的問題。

+2

您的括號內容不正確。例如'[dbo.Users]'應該是'[dbo]。[Users]' – DavidG

+0

哦,這很有道理,我會嘗試。謝謝 ! –

+0

沒有運氣,沒有改變我的問題。感謝您的嘗試。 –

回答

1

我的問題是,由於某種原因,當我運行Create-Migration時,它生成了一個引用表中不存在的字段的命令。一旦我修好了,它就完美了。