我创建了具有简单登录和注册功能的ASP .NET MVC应用程序,并尝试使用“代码优先”方法创建Entity Framework 6模型。我正在使用LocalDb创建数据库。但是,添加
时出现以下异常<contexts>
<context type="WebApplication1.DAL.ProjectContext, WebApplication1">
<databaseInitializer type="WebApplication1.DAL.ProjectInitializer, WebApplication1"/>
</context>
</contexts>
到Web.config文件。例外如下:
大多数堆栈跟踪为:
[TypeLoadException: Could not load type 'WebApplication1.DAL.ProjectContext' from assembly 'WebApplication1'.]
System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMarkHandle stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName, ObjectHandleOnStack type) +0
System.RuntimeTypeHandle.GetTypeByName(String name, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark, IntPtr pPrivHostBinder, Boolean loadTypeFromPartialName) +106
System.RuntimeType.GetType(String typeName, Boolean throwOnError, Boolean ignoreCase, Boolean reflectionOnly, StackCrawlMark& stackMark) +62
System.Type.GetType(String typeName, Boolean throwOnError) +46
System.Data.Entity.Internal.InitializerConfig.TryGetInitializer(Type requiredContextType, String contextTypeName, String initializerTypeName, Boolean isDisabled, Func`1 initializerArgs, Func`3 exceptionMessage) +47
[InvalidOperationException: Failed to set database initializer of type 'WebApplication1.DAL.ProjectInitializer, WebApplication1' for DbContext type 'WebApplication1.DAL.ProjectContext, WebApplication1' specified in the application configuration. See inner exception for details.]
System.Data.Entity.Internal.InitializerConfig.TryGetInitializer(Type requiredContextType, String contextTypeName, String initializerTypeName, Boolean isDisabled, Func`1 initializerArgs, Func`3 exceptionMessage) +327
System.Data.Entity.Internal.<>c__DisplayClass6.<TryGetInitializerFromEntityFrameworkSection>b__1(ContextElement e) +276
System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +223
这是ProjectContext.cs代码:
namespace WebApplication1.Models.DAL{
using System;
using System.Data.Entity;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using WebApplication1.Models;
using WebApplication1.Models.DAL;
public partial class ProjectContext : DbContext
{
public ProjectContext(): base()
{
}
public DbSet<PIP> PIP { get; set; }
public DbSet<Project> Project { get; set; }
}
}
这是ProjectInitializer.cs代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using WebApplication1.Models;
namespace WebApplication1.Models.DAL
{
public class ProjectInitializer:System.Data.Entity.DropCreateDatabaseIfModelChanges<ProjectContext>
{
protected override void Seed(ProjectContext context)
{
var projects = new List<Project>
{
new Project{ ProjectID=1, Name="New ADM", StartDate=new DateTime(2017,1,10), EndDate=new DateTime(2017,2,10),},
new Project{ ProjectID=2, Name="New ADM", StartDate=new DateTime(2016,5,20), EndDate=new DateTime(2016,6,20),},
new Project{ ProjectID=3, Name="New ADM", StartDate=new DateTime(2015,8,30), EndDate=new DateTime(2015,9,30),}
};
projects.ForEach(pr => context.Project.Add(pr));
context.SaveChanges();
var PIP = new List<PIP>
{
new PIP{ PIPID=1, ProjectID=1 },
new PIP{ PIPID=2, ProjectID=2 },
new PIP{ PIPID=3, ProjectID=3 },
};
PIP.ForEach(pip => context.PIP.Add(pip));
context.SaveChanges();
Database.SetInitializer<ProjectContext>(new ProjectInitializer());
}
}
}
答案 0 :(得分:0)
在创建代码第一个实体框架应用程序时,无需添加上下文:
在“代码优先”方法中,您专注于自己的领域 应用程序并开始为您的域实体创建类 比先设计您的数据库,然后创建类 符合您的数据库设计
这意味着,您不需要上下文(设计器)来访问您的数据库,您可以创建自己的类来与您的数据库进行通信。
请先在entityframework tutorial网站上阅读有关代码的更多信息。
答案 1 :(得分:0)
您为ProjectContext
中的ProjectInitializer
和web.config
类使用了错误的命名空间,它应该是WebApplication1.Models.DAL
而不是WebApplication1.DAL
。
<contexts>
<context type="WebApplication1.Models.DAL.ProjectContext, WebApplication1">
<databaseInitializer type="WebApplication1.Models.DAL.ProjectInitializer, WebApplication1"/>
</context>
</contexts>