【问题标题】:Entity Framework Database Initializer Fails实体框架数据库初始化程序失败
【发布时间】:2014-10-16 14:16:09
【问题描述】:

我一直在关注本教程 http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application,但我正在为我正在从事的项目定制它。

我在运行它时遇到了问题 - 它无法初始化数据库并在我访问需要使用数据库的控制器时给我这个错误:

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();
        }
    }
}

【问题讨论】:

  • 你能展示一下你的JustSpecItInitializer类是什么样子的吗?
  • 您是在同一个程序集中还是从不同程序集中加载初始化程序类?
  • 您使用哪个版本的 EF?
  • 添加了初始化程序。 EF 是 6.0 版。我想我是从同一个程序集中加载它的,我该如何检查?这一切都在一个项目中,该项目的属性告诉我程序集名称是 JustSpecIt。谢谢大家!

标签: c# asp.net asp.net-mvc entity-framework asp.net-mvc-4


【解决方案1】:

JustSpecItInitialiser 还是 JustSpecItInitializer ? 检查语法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-16
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 2015-01-15
    • 1970-01-01
    相关资源
    最近更新 更多