2015-10-26 102 views
0

我有我的解決方案2個項目,你可以看到:代碼首先在EF不創建數據庫

enter image description here

我想先用代碼來創建我的數據庫,所以我的代碼來創建模型是:

namespace CMSDataLayer 
{ 
    public class CMSDataContext : DbContext 
    { 
     public CMSDataContext() 
     : base("DefaultConnection") 
    { 
     this.Configuration.LazyLoadingEnabled = false; 
     this.Configuration.ProxyCreationEnabled = false; 

     Database.SetInitializer(
     new MigrateDatabaseToLatestVersion<CMSDataContext, CMSMigrationsConfiguration>() 
     ); 
    } 
     public DbSet<table1> Topics { get; set; } 
    } 
} 

public class CMSMigrationsConfiguration : DbMigrationsConfiguration<CMSDataContext> 
    { 

     public CMSMigrationsConfiguration() 
     { 
      this.AutomaticMigrationDataLossAllowed = true; 
      this.AutomaticMigrationsEnabled = true; 
     } 

    } 
    } 

我添加的連接字符串MVC p roject webconfig

運行數據庫後沒有創建。我在CMSDataContext類中放置了一個斷點,但斷點從未調用過。爲什麼?

我很早就在EF代碼中。

最好的問候

+0

我永遠不會使用這些標誌'this.AutomaticMigrationDataLossAllowed = true; this.AutomaticMigrationsEnabled = true;' –

+0

我將CMSdatalayer添加到我的mvc項目 –

+0

@EricJ。爲什麼親愛的朋友? –

回答

0

在您的項目你沒有手動在這裏所做的任何數據庫連接,它是如何顯示:

View -> Server Explorer Right click on Data Connections and select Add Connection…
如果你的避風港從服務器瀏覽器連接到數據庫之前,需要選擇Microsoft SQL Server作爲數據源

Here是一篇文章,說明如何添加EF

+0

我在CMSDataContext類中放置了一個斷點,但斷點從未調用過 –

0

據我所知,它會創建數據庫之前不使用它。 嘗試將實體添加到數據庫,然後查看它是否創建它。

可以使用直播功能,如果你想添加一些虛擬數據: seeding tutorial

+0

我在CMSDataContext類中放置了一個斷點,但斷點從未調用 –