无法基于DBContext创建强类型Razor视图 [英] Can't create strongly typed Razor View based on DBContext

查看:218
本文介绍了无法基于DBContext创建强类型Razor视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以根据强类型集合创建视图。

@inherits System.Web.Mvc.WebViewPage< IEnumerable< Data.ProductCategory>>

这很好用,

I can create a view based on a strongly typed collection.
@inherits System.Web.Mvc.WebViewPage<IEnumerable<Data.ProductCategory>>
This works fine,

当我根据基于DBContext的类创建一个剃刀视图时,
@inherits System.Web.Mvc。 WebViewPage< Data.MNG_Model>

应用程序抛出和错误:

CS0012:类型'System.Data.Entity.DbContext'在未引用的程序集中定义。

你必须添加一个对程序集'Microsoft.Data.Entity.Ctp,

When I create a razor view with based on the class that is based on the DBContext
@inherits System.Web.Mvc.WebViewPage<Data.MNG_Model>
The App throws and error:
CS0012: The type 'System.Data.Entity.DbContext' is defined in an assembly that is not referenced.
You must add a reference to assembly 'Microsoft.Data.Entity.Ctp,

的引用嗯我显然有对CTP dll的引用,因为所有这些优秀代码的第一个东西都可以工作

,直到我尝试创建基于DBContext的强类型视图。

Well I obviously have references to the CTP dll because all this great code first stuff works
until I try and create the strongly typed view based on DBContext.

这是我的模型。


  public class MNG_Model : DbContext
  {
    public DbSet<ProductCategory> ProductCategories { get; set; }
    public DbSet<PriceCategory> PriceCategories { get; set; }
  }
  public class ProductCategory
  {
    public int ID { get; set; }
    public string Description { get; set; }
    public virtual ICollection<Product> Products { get; set; }
  }
  public class PriceCategory
  {
    public int ID { get; set; }
    public string Description { get; set; }
    public virtual ICollection<Product> Products { get; set; }
  }

推荐答案

您需要在web.config文件中添加一个条目,让MVC知道在编译页面时引用CTP4程序集:

You need to add an entry to your web.config file to let MVC know to reference the CTP4 assembly when it compiles pages:


 <system.web>
  <compilation debug="true" targetFramework="4.0">
   <assemblies>
    <add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
    <add assembly="Microsoft.Data.Entity.CTP, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
   </assemblies>
  </compilation>


这篇关于无法基于DBContext创建强类型Razor视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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