2014-10-16 70 views
0

我一直在關注本教程http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application,但是爲我正在處理的項目定製它。實體框架數據庫初始化失敗

我有,當我雖然運行的問題 - 它無法初始化數據庫,並給了我這個錯誤,當我訪問控制器,它需要使用DB:

Failed to set database initializer of type 'JustSpecIt.DAL.JustSpecItInitializer, JustSpecIt' for DbContext type 'JustSpecIt.DAL.JustSpecItAppContext, JustSpecIt' specified in the application

我的初始化和上下文文件位於DAL文件夾中。在我的webconfig文件我有:

<entityFramework> 
<contexts> 
<context type="JustSpecIt.DAL.JustSpecItAppContext, JustSpecIt"> 
    <databaseInitializer type="JustSpecIt.DAL.JustSpecItInitializer, JustSpecIt" /> 
</context> 

而對於我的連接字符串我有:

<connectionStrings> 
<add name="JustSpecItAppContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=JustSpecIt;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/> 
</connectionStrings> 

我是新來這個,所以讓我知道如果有更多的信息是必需的。

按照要求,這裏是我的初始化類:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Data.Entity; 
using JustSpecIt.Models; 


namespace JustSpecIt.DAL 

{

public class JustSpecItInitialiser : System.Data.Entity.DropCreateDatabaseIfModelChanges<JustSpecItAppContext> 
    { 
     protected override void Seed(JustSpecItAppContext context) 
     { 
      var Projects = new List<Project> 
      { 
      new Project {ProjectName="Just Spec It", ClientID = "GR Corp", Description="Build Just Spec It Application"} 
      }; 

      Projects.ForEach(s => context.Projects.Add(s)); 
      context.SaveChanges(); 

      var UseCases = new List<UseCase> 
      { 
       new UseCase {ProjectID = 1, ActorID = 1, Title = "Print Specification"} 
      }; 

      UseCases.ForEach(s => context.UseCases.Add(s)); 
      context.SaveChanges(); 

      var Steps = new List<Step> 
      { 
       new Step {UseCaseID = 1, Content = "This is step 1"}, 
       new Step {UseCaseID = 1, Content = "This is step 2"}, 
       new Step {UseCaseID = 1, Content = "This is step 3"}, 
      }; 

      Steps.ForEach(s => context.Steps.Add(s)); 
      context.SaveChanges(); 

      var Queries = new List<Query> 
      { 
       new Query {UseCaseID = 1, QueryDescription = "We need to add more details to this Use Case!"} 
      }; 

      Queries.ForEach(s=> context.Queries.Add(s)); 
      context.SaveChanges(); 

      var Actors = new List<Actor> 
      { 
       new Actor {Title = "Business Analyst", Description = "This group will interact with the application to generate the use cases. They will follow Cockburn's writing process to produce the use cases in an effective way!"}, 
       new Actor {Title = "End System User", Description = "These users will use the application for two primary purposes. The first is to submit uses case titles (which in fact will be their goals). After the Business Analyst has entered full details into the application about how these goals will be carried out, the End System Users will use the application again to review and comment on them."} 
      }; 

      Actors.ForEach(s => context.Actors.Add(s)); 
      context.SaveChanges(); 
     } 
    } 
} 
+0

你可以顯示你的'JustSpecItInitializer'類的樣子嗎? – DavidG 2014-10-16 14:20:33

+0

你是否在同一個程序集或不同程序集中加載初始化程序類? – SDK 2014-10-16 14:25:51

+0

你使用的版本是EF嗎? – 2014-10-16 14:26:02

回答

3

JustSpecItInitiali 小號 ER或JustSpecItInitiali ž呃? 檢查語法。

+0

好地方!想不出來...... – DavidG 2014-10-16 14:36:15

+0

謝謝@ k4s0r42! – gazrolo4 2014-10-16 14:39:53

相關問題