如何使用Entity Framework 6解决System.TypeInitializationException [英] How to solve System.TypeInitializationException with Entity Framework 6
问题描述
我开始在笔记本电脑上开发MVC应用程序,该应用程序运行VS2013和SQL Server 2008 R2。我将这个项目复制到运行Visual Studio 2013,SQL Server 2012 Developer Edition的桌面计算机上。
当我从桌面运行这个MVC应用程序时,我得到以下内容我设置初始化程序的行上的错误,如下所示:
public class MvcApplication:System.Web.HttpApplication
{
protected void Application_Start()
{
Database.SetInitializer< LiquorContext>( new LiquorContextInitializer()); // 此处发生异常
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
这是我得到的例外:
附加信息:'System.Data.Entity.DropCreateDatabaseAlways`1'的类型初始值设定项引发异常。
我用Nuget控制台更新我的软件包如下:
Update-Package -reinstall EntityFramework
Update-Package -reinstall Microsoft.AspNet.Identity.EntityFramework
Update-Package -reinstall Microsoft.Owin.Host.SystemWeb
Update-Package -reinstall Microsoft.Owin .Security.Cookies
到目前为止我的尝试都没有,我无法弄清楚需要改变什么才能解决这个问题。
任何帮助都将不胜感激。
注意:我注意到实体框架版本已从版本6.1.2更改到6.1.3,但我在更新实体框架包之前收到了这个错误。
我在项目中使用Moq。所以我安装了它然后安装了它的依赖项。
有可能你是一些依赖。
I started developing an MVC app on my laptop, which was running VS2013, and SQL server 2008 R2. I copied this project over to my desktop computer running Visual Studio 2013, SQL server 2012 Developer Edition.
When I run this MVC app from my Desktop, I get the following error on the line where I set the Initializer, as can be seen below:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
Database.SetInitializer<LiquorContext>(new LiquorContextInitializer()); //Exception happens here
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
This is the exception I get:
Additional information: The type initializer for 'System.Data.Entity.DropCreateDatabaseAlways`1' threw an exception.
I have used Nuget console to update my packages, as follow:
Update-Package -reinstall EntityFramework
Update-Package -reinstall Microsoft.AspNet.Identity.EntityFramework
Update-Package -reinstall Microsoft.Owin.Host.SystemWeb
Update-Package -reinstall Microsoft.Owin.Security.Cookies
None of my attempts have worked so far, and I cannot figure out what needs to change to fix this.
Any help would be appreciated.
NB: I have noticed the Entity Framework version has changed from version 6.1.2 to 6.1.3, but I was receiving this error before I updated Entity Framework Package.
I was using Moq with my project. So I installed it and then installed its dependencies.
There is a chance that you are some dependencies.
这篇关于如何使用Entity Framework 6解决System.TypeInitializationException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!