2014-02-05 28 views
1

ASP.NET MVC4 - 第7章(閱/亞當·弗里曼)第175頁實體框架 - SportsStore - 圖書ASP.NET MVC 4 - 第七章

有人做過完成的例子嗎?

我想在本書中做這個例子,是準備數據庫部分下的部分。我面臨的問題是View沒有列出任何產品,我的Table Products有一些行但視圖沒有顯示任何內容。即使我註釋掉了連接字符串,它也沒有顯示任何錯誤。

我的產品類(SportsStore.domain)

namespace SportsStore.Domain.Entities 
{ 
    public class Product 
    { 
     public int ProductID { get; set; } 
     public string Name { get; set; } 
     public string Description { get; set; } 
     public decimal Price { get; set; } 
     public string Category { get; set; } 
    } 
} 

Ninject廠

namespace SportsStore.WebUI.Infrastructure 
{ 
    public class NinjectControllerFactory : DefaultControllerFactory 
    { 
     private IKernel ninjectKernel; 

     public NinjectControllerFactory() { 
      ninjectKernel = new StandardKernel(); 
      AddBindings(); 
     } 

     protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) 
     { 
      return controllerType == null 
       ? null 
       : (IController)ninjectKernel.Get(controllerType); 
     } 

     private void AddBindings() 
     { 


      ninjectKernel.Bind<IProductRepository>().To<EFProductRepository>(); 

     } 
    } 
} 

Iproduct庫

namespace SportsStore.Domain.Abstract 
{ 
    public interface IProductRepository 
    { 
     IQueryable<Product> Products { get; } 
    } 
} 

回答

0

我造成這個問題上我意外,因爲我忘記要繼承創建EFDbContext類時的DbContext。

1

好吧,我也是在與這個鬥爭巴士上,並在網上搜索了2天的時間,我想出了一個讓所有東西都顯示出來的工作。

本書要求您在第181頁的Web.Config文件中添加連接字符串,該文件將替換默認的連接字符串。

<connectionString> <add name="EFDbContext"... /> </connectioString>

這本書是要求一個 「代碼優先」 的方法。我無法得到這個工作,所以我通過使用「數據庫優先」方法explanation of the two methods採用了不同的方法。

要做到這一點,你必須的路徑,在Web.config文件中的數據庫從以前的一個

<add name="EFDbContext" connectionString="Data Source=(localdb)\v11.0;Initial Catalog=SportsStore;Integrated Security=True" providerName="System.Data.SqlClient" /> 

改成這樣

<add name="EFDbContext" connectionString="Data Source=YourDataBaseHere;Initial Catalog=SportsStore;Integrated Security=True" providerName="System.Data.SqlClient"/> 

你可以通過右側的連接路徑點擊你想要的數據庫並選擇屬性,它將是標有連接字符串的那個。

我希望這可以幫助任何人閱讀本書!