dyarosh
asked on
The model item passed into the dictionary is of type 'System.Collections.Generic.List' but this dictionary requires a model item of type 'System.Collections.Generic.IEnumberable'
I have an MVC 4 application that has a model developed using EF 5.0 (Code First). I am investigating TDD and had to make changes to the code generated by the model in order to get my unit tests to work. Now that my unit tests work I am getting the following error when I try and run the application.
Here is my controller:
Here is the model definition in my view:
Here is my Interface definition:
Here is the AppCatalogContainer definition:
I'm not sure what change I need to make to the View or the Controller to get this to work. Everything worked fine until I made the changes for the unit testing.
Any help is greatly appreciated!
System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[AppCatalog.Models.AppCatalog_Database]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[AppCatalogData.AppCatalog_Database]'.
Here is my controller:
namespace AppCatalog.Controllers
{
public class DBCatalogController : Controller
{
private IAppCatalog db { get; set; }
public DBCatalogController(IAppCatalog dataContext = null)
{
db = dataContext ?? new AppCatalogContainer();
}
public DBCatalogController()
{
db = new AppCatalogContainer();
}
//
// GET: /DBCatalog/
public ViewResult Index()
{
ViewBag.Title = "Application Catalog -> Database Catalog";
var appcatalog_database = db.AppCatalog_Database.Include(a => a.AppCatalog_ServerType);
return View(appcatalog_database.ToList());
}
}
}
Here is the model definition in my view:
@model IEnumerable<AppCatalogData.AppCatalog_Database>
Here is my Interface definition:
namespace AppCatalog.Models
{
public interface IAppCatalog
{
IDbSet<AppCatalog_Database> AppCatalog_Database { get; set; }
IDbSet<AppCatalog_ServerType> AppCatalog_ServerType { get; set; }
IDbSet<AppCatalog_DBConnection> AppCatalog_DBConnection { get; set; }
IDbSet<AppCatalog_Environment> AppCatalog_Environment { get; set; }
IDbSet<AppCatalog_PIIData> AppCatalog_PIIData { get; set; }
IDbSet<AppCatalog_Application> AppCatalog_Application { get; set; }
IDbSet<AppCatalog_ApplicationTable> AppCatalog_ApplicationTable { get; set; }
int SaveChanges();
void Dispose();
}
Here is the AppCatalogContainer definition:
namespace AppCatalog.Models
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class AppCatalogContainer : DbContext, IAppCatalog
{
public AppCatalogContainer()
: base("name=AppCatalogContainer")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public IDbSet<AppCatalog_Database> AppCatalog_Database { get; set; }
public IDbSet<AppCatalog_ServerType> AppCatalog_ServerType { get; set; }
public IDbSet<AppCatalog_DBConnection> AppCatalog_DBConnection { get; set; }
public IDbSet<AppCatalog_Environment> AppCatalog_Environment { get; set; }
public IDbSet<AppCatalog_PIIData> AppCatalog_PIIData { get; set; }
public IDbSet<AppCatalog_Application> AppCatalog_Application { get; set; }
public IDbSet<AppCatalog_ApplicationTable> AppCatalog_ApplicationTable { get; set; }
}
}
I'm not sure what change I need to make to the View or the Controller to get this to work. Everything worked fine until I made the changes for the unit testing.
Any help is greatly appreciated!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I found the problem on my own.
So, I think these lines of code are the issue:
Open in new window
Instead of doing the .ToList(), do as documented here:
http://msdn.microsoft.com/en-us/library/bb739071(v=vs.110).aspx
I would do:
Open in new window
...or something like that. I don't have your code, so I can't fiddle with it.