MVC3 Add Controller Scaffolding Error - Unsupported Context Type

耗尽温柔 提交于 2019-12-13 12:01:33

问题


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!


回答1:


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.




回答2:


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.




回答3:


I got this error message when I had mixed the "model class" and "data context class".




回答4:


I am not sure if this is exactly what you were looking for.. but I noticed the same error myself.. Google was not much help and I wound up spotting the issue myself.

Possible Solution:

If you are creating a Model that contains an EF (Entity Framework) class that extends to another class.. make sure that you are extending that constructor to the intended EF class and not the Model Class file unintentionally..

I apologize for the poor explanation but I am new to this.. Let me know if this helps.

Regards




回答5:


I debugged this problem in WinDbg and It's caused by the following Microsoft.VisualStudio.Web.Mvc.Scaffolding.BuiltIn.EntityFrameworkServices method:

internal static bool IsValidContextType(Type contextType)
{
    return (typeof(ObjectContext).IsAssignableFrom(contextType) || ((contextType.BaseType != null) && contextType.BaseType.FullName.Equals("System.Data.Entity.DbContext", StringComparison.OrdinalIgnoreCase)));
}

This method is returning false because it is being passed the wrong Type. I'm not sure where they come from, but there are multiple DLLs loaded inside VS that contain implementations of the context type, but only one of them is Derived From DbContext - all the others are derived from System.Object. these bogus types' assemblies are in my '%localappdata%\assembly' directory, so they've been auto-generated by some tool and loaded into VS.

The bug is caused by the fact that Microsoft.VisualStudio.Web.Mvc.Util.TypeHelper.GetType only filters by Type.FullName. in order to find the correct Type it's also necessary to filter by IsValidContextType().

Ok, something really weird is going on. I have 2 partial classes for my DbContext-derived class (most of it is auto-generated by a .tt script, and some is hand-crafted). when i try to add a controller, VS adds new properties to my partial class, then it builds that part of the partial class (only the manual part, and it doesn't use the base class). then it loads that DLL that it built from half the model class into memory, then it fails the base-class check above.

weird.

bottom line: try removing partial classes of your model context, if you have one.




回答6:


I got the same issue with EF. I am using the VS 2012 The reason for my case was.. this auto generation process (Scaffolding) seems not recognize the partial class concept.

I used the model first approach and I have used inheritance with the entities. Ex: entity “B” and “C” is inherited from “A”

So in my generated model class “DataModelContainer” which is inherited from “DbContext”, There is no definition for “DbSet” and “DbSet” i.e: the following two lines were not there

public DbSet<B> B { get; set; }
public DbSet<C> C { get; set; }

Generated “DataModelContainer” class I a partial class, so I completed the other part, using the concept of partial class. And this would be a problem for Scaffolding.

My workaround was, just removed the partial class I added manually. And added the definitions for “DbSet” and “DbSet” in to the auto generated class. The problem with this solution is, I have to repeat the same thing when I regenerate the model classes.



来源:https://stackoverflow.com/questions/9116601/mvc3-add-controller-scaffolding-error-unsupported-context-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!