Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 Add Controller Scaffolding Error - Unsupported Context Type

I'm creating a new MVC 3 application.
Steps taken:
1. Added new model by right clicking on Models and adding "LINQ to SQL Classes"
2. Dragged tables from Server Explorer to my new dbml layout and saved
3. Right click on Controllers->Add->Controller
4. Enter the following information:

Controller name: UserController
Template: Controller with read/write actions and views, using Entity Framework
Model Class: aspnet_User (TierPay)
Data Context Class: AgricultureDataContext (TierPay)
Views: Razor (VBHTML)

5.Click Add
6.Get the following error:
Unsupported context type.

I've Googled around and haven't found any answers. Thanks!

like image 287
fhilton Avatar asked Feb 02 '12 17:02

fhilton


2 Answers

I was getting this error when working with the Entity Data Model instead of Linq to SQL. I had created the model from an existing SQL Server database. The problem was caused by selecting the wrong Data Context Class in the Add Controller dialog. That values needs to be the top level class, in my case the one with "Entitites" as part of the name. I hope this helps.

Add new Controller to MVC project From Entity Models

like image 170
Ken Mc Avatar answered Nov 12 '22 18:11

Ken Mc


This dialog is confusing at first when using the Entity Framework Database first option.

If you are using DB first then both the "model class" dropdown and the "data context class" dropdown will appear to have the same class names.

If you are for instance, attempting to create a controller bound to a model of type "User" lets say you will see "User" both in the model drop down list as well as in the "Data Context" part of the dialog.

In the "model class" part of the dialog you want to select your model (User in our example here).

In the "Data Context class" part of the dialog you do not want to also select "User". Instead you want to select the class that is in your EDMX file that inherits from ObjectContext. There will be one class like this in the drop down list if you are using DB first. You will see this class in the list and I have no idea why the other classes are in the list. I think that may be a small UI defect from Microsoft.

If you do not know what this class is, simply go to your EDMX model and click the designer.cs file associated with it. At the top of this code you will see the class that inherits from ObjectContext. This is the class you want to select.

like image 12
Matt Avatar answered Nov 12 '22 20:11

Matt