Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架错误ReflectionTypeLoadException_C#_Asp.net Mvc_Entity Framework_Asp.net Mvc 4_Dbcontext - Fatal编程技术网

C# 实体框架错误ReflectionTypeLoadException

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

当我试图在数据库中保存数据时,我遇到以下错误:

无法加载一个或多个请求的类型。有关详细信息,请检索LoaderExceptions属性。 使用EF对象的所有操作都会发生此错误,例如,当我执行此操作时(在返回步骤中):

或此(在AddObject步骤上):


好的,因此当您收到此消息时,错误可能会有所不同。我的问题是我安装了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.
}