Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC5 and EF6.1.3 Scaffolding problems

I have a problem when trying to build a view using scaffolding via the "Add View" function within a controller using MVC5 with Entity Framework 6.1.3.

However, using Entity Framework 5.0.0 everything works perfectly ~ I can build a view successfully.

Using Entity Framework 6.1.3

When I select the "Add View" function I get the following error

"There was an error running the selected code generator. 'Unable to retrieve metadata for 'xxx'. Could not find the CLR type for 'xxx'.

My environment is VS2013 Ultimate (Update4), target framework 4.5.1 ,C#, MVC5, EF6.1.3 Database First ObjectContext, SQL Server 2008.

My DAL and MVC app are in different projects.

I have checked that both are using the same version of EF6.1.3 by uninstalling and reinstalling via both Nuget and the Package Manager Console Command line prompts and that all other references are correct.

My Model class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SysviewData;

namespace SysviewWebEF613.Models
{
    public class DisplaySysoutModel
    {
        SysviewEntities context = new SysviewEntities();

        public IEnumerable<vw_soServerInfo> GetSysouts()
        {
            return context.vw_soServerInfo.ToList();
        }
    }
}

My Controller

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SysviewWebEF613.Models;

namespace SysviewWebEF613.Controllers
{
    public class DisplaySysoutsController : Controller
    {
        // GET: DisplaySysouts
        DisplaySysoutModel model = new DisplaySysoutModel();
        public ActionResult Index()
        {
            return View(model.GetSysouts());
        }
    }
}

Regarding EF6.1.3

1) I am using EF database first modelling. For historical reasons I am using ObjectContext.

2) I have tried using both Code Generation Strategies ~ "Legacy ObjectContext" and "T4" ~ (clean/rebuild etc) neither work ~ I get the same error message.

"There was an error running the selected code generator. 'Unable to retrieve metadata for 'xxx'. Could not find the CLR type for 'xxx'.

However, if I use EF5.0.0 ~ I am able to build my View successfully.

Background Info & why I do not want to use EF5.0.0

1) I am developing a new web front end project using MVC5 that will replace and existing web app in a solution that contains 23 other projects ~ Currently using EF6.1.3 and have been using EF6.x.x for around 2 years now.

So, you can understand my reluctance to want to change back to using EF5.0.0?

2) So, for the above reason ~ this is why I am trying to use EF6.1.3 and not EF5.0.0.

Is it as simple as EF6.xxx does not support MVC5 scaffolding when using EF ObjectContext?

I have been battling with this for days now ~ I have researched this problem extensively and although I have seen similar scaffolding problems reported a couple of years ago (MVC4 & EF6xx in compatibility) ~ I understood (maybe wrongly) that MVC5 and EF6.1.3 now resolved those issues and I have not seen anyone else have a problem building a View ~ Indeed ~ if I use the same code ~ but use EF5.0.0 ~ everything works.

I've double checked my connection strings in my app.config (DAL) and Web.config (MVC project) ~ all seem correct.

Do you have any ideas or have you experienced anything similar and what did you do to fix this problem?

like image 519
Jiving Rockabilly Avatar asked May 20 '15 15:05

Jiving Rockabilly


1 Answers

The solution is actually very simple.

Leave the "Data context class:" menu option empty

as follows

enter image description here

I only picked up on this when I was comparing my MVC5 / EF 6.1.3 solution to my MVC5 / EF 5.0.0 solution.

~ you do not get the "Data context class:" menu with EF 5.0.0, so on a hunch, I tried to create a view in my EF 6.1.3 solution leaving "Data context class:" menu empty and BINGO ~ View generated successfully.

In the last two minutes I have since created two other models each with a controller and successfully built render-able Views using scaffolding.

like image 180
Jiving Rockabilly Avatar answered Nov 16 '22 04:11

Jiving Rockabilly