0

我試圖在添加需要在數據庫中的表的幾個類後更新我的數據庫。嘗試更新時出錯 - 數據庫代碼優先

,我發現了以下錯誤:

There is already an object named 'Blogs' in the database. 

我的數據上下文這個樣子的(3最後一行是我需要添加到我的數據庫表):

public class WowDataContext : DbContext 
{ 
    public WowDataContext() 
     : base("name=DefaultConnection") 
    { } 
    public DbSet<Genre> Ganres { get; set; } 
    public DbSet<TakeAway> TaKeAways { get; set; } 

    public DbSet<Gellery> Galleries { get; set; } 
    public DbSet<katering> Katring { get; set; } 
    public DbSet<Blog> Blogs { get; set; } 
    public DbSet<Events> events { get; set; } 
    //i need to add this folowing 3 tables 
    public DbSet<Order> orders { get; set; } 
    public DbSet<OrderDetail> OrderDetails { get; set; } 
    public DbSet<Cart> Carts { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     base.OnModelCreating(modelBuilder); 
    } 
} 

的孔更新Db的是這樣的:

[http://pastebin.com/CTwbz6Wj1

和我的數據庫遷移看起來像這樣圖片 [遷移表] [2]`

Db table

+0

您需要運行update-database命令。 http://stackoverflow.com/questions/18308831/how-to-update-model-and-database-with-code-first-approach-in-asp-net-mvc – Brian

+0

是的,我已經嘗試過,我也重新運行此:添加遷移201403251531583_InitialCreate 重新腳手架遷移'201403251531583_InitialCreate'。 只有設計器代碼遷移'201403251531583_InitialCreate'被重新腳手架。要重新搭建整個遷移,請使用-Force參數..它沒有幫助 – Danny

回答

0

你可以嘗試以下方法:

  1. 刪除所有移民在遷移文件夾(注意 - 不要刪除配置類文件)
  2. 運行add-migration命令
  3. 運行update-database命令

如果這也不行,如果這是在測試環境中,那麼你也可以做到以下幾點:

  1. 刪除數據庫
  2. 刪除所有遷移您的遷移文件夾(請小心 - 不要刪除配置類文件)
  3. 運行add-migration命令
  4. 運行update-database命令
相關問題