为什么不MVC3我的脚手架外键列 [英] Why is MVC3 not scaffolding my foreign key columns

查看:133
本文介绍了为什么不MVC3我的脚手架外键列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用code首先使用MVC 3 EF 4.1和我下面斯科特Guthries教程<一个href=\"http://weblogs.asp.net/scottgu/archive/2011/05/05/ef-$c$c-first-and-data-scaffolding-with-the-asp-net-mvc-3-tools-update.aspx\" rel=\"nofollow\">http://weblogs.asp.net/scottgu/archive/2011/05/05/ef-$c$c-first-and-data-scaffolding-with-the-asp-net-mvc-3-tools-update.aspx.

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.

我已经追查为什么不被显示的列是因为T4模板的原因......它是失败的检查,看它是否是为了显示属性作为列可绑定类型。

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);
}

在哪里bindableNonPrimitiveTypes是一个固定的列表:

Where bindableNonPrimitiveTypes is a fixed list:

static Type[] bindableNonPrimitiveTypes = new[] {
typeof(string),
typeof(decimal),
typeof(Guid),
typeof(DateTime),
typeof(DateTimeOffset),
typeof(TimeSpan),
};

我刚刚安装VS2010 SP1,EF 4.1和教程引用的MVC3工具更新。
我敢肯定,我已经照着所有的步骤...

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).

我的,为什么它没有工作你的情况最好的猜测是,也许你错过了从产品的类别id 属性类,或者你叫别的东西。脚手架检测FK的关系,有必要为你的实体既具有导航属性(在这种情况下,类别,类型类别)和外键属性(在这种情况下,类别id 类型 INT ) - 如果没有那些不会推断关系,因此你不会得到下拉

My best guess about why it didn't work in your case is that perhaps you missed the CategoryID property from the Product 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 type Category) and a "foreign key" property (in this case CategoryID of type int) - without those it won't infer the relationship and hence you wouldn't get the dropdown.

在情况下,它可以帮助,这里是完整的code表示,你可以复制并粘贴到您的项目模型类:

In case it helps, here's the full code for the model classes that you can copy and paste into your project:

public class Product
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int CategoryID { get; set; }
    public decimal? UnitPrice { get; set; }
    public int UnitsInStock { get; set; }

    public virtual Category Category { get; set; }
}

public class Category
{
    public int CategoryID { get; set; }
    public string Name { get; set; }
    public virtual ICollection<Product> Products { get; set; }
}

public class StoreContext : DbContext
{
    public DbSet<Product> Products { get; set; }
    public DbSet<Category> Categories { get; set; }
}

记住使用添加控制器窗口之前编译code,否则也不会意识到你已经改变了code。

Remember to compile your code before using the "Add Controller" window, otherwise it will not realise that you've changed the code.

这篇关于为什么不MVC3我的脚手架外键列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆