Ivan Talalaev
Ivan Talalaev

Reputation: 6494

Unable to cast object of type 'System.Data.Entity.DynamicProxies.Assignment...'

Additional information: Unable to cast object of type 'System.Data.Entity.DynamicProxies.Assignment_20539BEAE4B141780E3B2D295A6858EAA8B5E9C0AD25E8A54154D0AB1E7DFA28' to type 'AssignmentSystem.Models.AssignmentViewModel'.

I have mvc application with the following model classes

/// class for generating EF CodeFirst Db

public class Assignment
{
    public Int64 AssignmentId { get; set; }


    public Int64? ApplicationUserId { get; set; }


    public string Text { get; set; }


    public DateTime CreationDate { get; set; }


    public virtual List<User> Coordinators { get; set; }


    public virtual List<User> Executors { get; set; }


    public virtual List<User> DelayResponsible { get; set; }

    ....
    ....
}

/// this class for interaction with view
public class AssignmentViewModel : Assignment
{
    public IEnumerable<SelectListItem> AvaliableUsers { get; set; }
    public IEnumerable<SelectListItem> AvaliableExecutors { get; set; }
    public IEnumerable<SelectListItem> AvaliableCoordinators { get; set; }
    public IEnumerable<string> SelectedUsers { get; set; }
    public IEnumerable<string> SelectedExecutors { get; set; }
    public IEnumerable<string> SelectedCoordinators { get; set; }


    public AssignmentViewModel() : base ()
    {
        AvaliableUsers = new List<SelectListItem>();
        AvaliableExecutors = new List<SelectListItem>();
        AvaliableCoordinators = new List<SelectListItem>();

        SelectedUsers = new List<string>();
        SelectedExecutors = new List<string>();
        SelectedCoordinators = new List<string>();
    }
}

My application worked perfectly, but after certain moment (just recompile) POOOF! It doesn't work.

I get the error mentioned above in code:

// GET: /Assignment/Edit/5
    public ActionResult Edit(long? id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        AssignmentViewModel assignment = (AssignmentViewModel)db.Assignments.Include(a => a.Attachments)
            .Include(a => a.Coordinators)
            .Include(a => a.DelayResponsible)
            .Include(a => a.Executors)
            .Include(a => a.MemberComments)
            .Include(a => a.Initiator).First<Assignment>(f => f.AssignmentId == id); //here
        ....
        ....
    }

And this:

AssignmentViewModel assignV = new AssignmentViewModel();
Assignment assignment = new Assignment();
assignV = (AssignmentViewModel) assignment;

Doesn't work also;

I tried to clear VS 2013 temp files as it written here Also tried to rebuilt from scratch in another directory whole solution. Tried this

Thanks in advance.

Upvotes: 2

Views: 6155

Answers (1)

Anthony Chu
Anthony Chu

Reputation: 37520

The code is trying to cast an Assignment object (in the first exception, it's actually a dynamic proxy object that inherits from Assigment) to an AssignmentViewModel. Because AssignmentViewModel inherits from Assignment, it's trying to downcast a less specific type to a more specific type; and that's not possible.

Look into using something like AutoMapper to map your model to a view model.

Upvotes: 3

Related Questions