C# 实体框架错误ReflectionTypeLoadException
当我试图在数据库中保存数据时,我遇到以下错误: 无法加载一个或多个请求的类型。有关详细信息,请检索LoaderExceptions属性。 使用EF对象的所有操作都会发生此错误,例如,当我执行此操作时(在返回步骤中): 或此(在AddObject步骤上):C# 实体框架错误ReflectionTypeLoadException,c#,asp.net-mvc,entity-framework,asp.net-mvc-4,dbcontext,C#,Asp.net Mvc,Entity Framework,Asp.net Mvc 4,Dbcontext,当我试图在数据库中保存数据时,我遇到以下错误: 无法加载一个或多个请求的类型。有关详细信息,请检索LoaderExceptions属性。 使用EF对象的所有操作都会发生此错误,例如,当我执行此操作时(在返回步骤中): 或此(在AddObject步骤上): 好的,因此当您收到此消息时,错误可能会有所不同。我的问题是我安装了EF5,而我引用的另一个项目有4.4,并且存在冲突。 在异常块中执行此代码将帮助您获得确切的消息: using System.IO; using System.Reflectio
好的,因此当您收到此消息时,错误可能会有所不同。我的问题是我安装了EF5,而我引用的另一个项目有4.4,并且存在冲突。 在异常块中执行此代码将帮助您获得确切的消息:
using System.IO;
using System.Reflection;
try
{
//The code that causes the error goes here.
}
catch (ReflectionTypeLoadException ex)
{
StringBuilder sb = new StringBuilder();
foreach (Exception exSub in ex.LoaderExceptions)
{
sb.AppendLine(exSub.Message);
if (exSub is FileNotFoundException)
{
FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
if(!string.IsNullOrEmpty(exFileNotFound.FusionLog))
{
sb.AppendLine("Fusion Log:");
sb.AppendLine(exFileNotFound.FusionLog);
}
}
sb.AppendLine();
}
string errorMessage = sb.ToString();
//Display or log the error based on your application.
}
最初写在这篇文章中:的可能副本看起来像是UserInfoes禁用了延迟加载。确保它的UserInfoes是虚拟的。@Smith h.Neil我在发布之前检查了那篇文章,它没有帮助。那么,
LoaderException
属性中有什么?
public bool AddAdditionalUserInfo(int userId, string firstName, string lastName, string emailAddress)
{
try
{
UserInfo userInfo = new UserInfo();
userInfo.UserId = userId;
userInfo.FirstName = firstName;
userInfo.LastName = lastName;
userInfo.EmailAddress = emailAddress;
userInfo.UserInfoId = 0;
_db.UserInfoes.AddObject(userInfo);
_db.SaveChanges();
return true;
}
catch (Exception ex)
{
this._lastError = ex.Message;
return false;
}
}
using System.IO;
using System.Reflection;
try
{
//The code that causes the error goes here.
}
catch (ReflectionTypeLoadException ex)
{
StringBuilder sb = new StringBuilder();
foreach (Exception exSub in ex.LoaderExceptions)
{
sb.AppendLine(exSub.Message);
if (exSub is FileNotFoundException)
{
FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
if(!string.IsNullOrEmpty(exFileNotFound.FusionLog))
{
sb.AppendLine("Fusion Log:");
sb.AppendLine(exFileNotFound.FusionLog);
}
}
sb.AppendLine();
}
string errorMessage = sb.ToString();
//Display or log the error based on your application.
}