문제

When I'm trying to save data in database I'm getting this error:

Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

That error is happening for all the operations with EF object for example when I do this (on return step):

public User GetUser(int userId)
        {
            try
            {
                return _db.Users.FirstOrDefault(x => x.UserId == userId && !x.IsDeleted);
            }
            catch (Exception ex)
            {
                this._lastError = ex.Message;
                return null;
            }
        }

or this (on AddObject step):

   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;
            }
        }
도움이 되었습니까?

해결책

ok, so the errors can vary when you got this message. My problem was that I had EF 5 installed and the other project which I had referenced had 4.4 and there was a conflict. Executing this code in the exception block will help you get the exact message:

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.
}

Originally written in this post: Error message 'Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.'

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top