Frage

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;
            }
        }
War es hilfreich?

Lösung

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top