I'm trying to use MVC 3 with EF 4.1 using code first and am following Scott Guthries tutorial http://weblogs.asp.net/scottgu/archive/2011/05/05/ef-code-first-and-data-scaffolding-with-the-asp-net-mvc-3-tools-update.aspx.
The issue I'm having is that when I create the products controller and the related scaffolded views, there is no "category" column being created in any of the views ("edit", "create", "index" etc), which according to the tutorial should be created.
I've traced the reason why the column is not being shown is because of the t4 templates... it is failing a check to see if it is a bindable type in order to display the property as a column.
The logic for checking if it is bindable is:
bool IsBindableType(Type type) {
return type.IsPrimitive || bindableNonPrimitiveTypes.Contains(type);
}
Where bindableNonPrimitiveTypes is a fixed list:
static Type[] bindableNonPrimitiveTypes = new[] {
typeof(string),
typeof(decimal),
typeof(Guid),
typeof(DateTime),
typeof(DateTimeOffset),
typeof(TimeSpan),
};
I have just installed VS2010 sp1, EF 4.1 and the MVC3 Tools Update referenced by the tutorial. I'm sure I've followed all the steps...
Where am I going wrong/What am I missing?
I believe that it does work as described in the tutorial - I just went through that tutorial right now and got the expected result (it did scaffold a "Category" column and drop-down list).
My best guess about why it didn't work in your case is that perhaps you missed the
CategoryID
property from theProduct
class, or maybe you called it something else. For scaffolding to detect the FK relationship, it's necessary for your entity to have both a "navigation" property (in this case,Category
, of typeCategory
) and a "foreign key" property (in this caseCategoryID
of typeint
) - without those it won't infer the relationship and hence you wouldn't get the dropdown.In case it helps, here's the full code for the model classes that you can copy and paste into your project:
Remember to compile your code before using the "Add Controller" window, otherwise it will not realise that you've changed the code.