【问题标题】:Entity Framework - SportsStore - Book ASP.NET MVC 4 - Chapter7Entity Framework - Sports Store - Book ASP.NET MVC 4 - Chapter 7
【发布时间】:2014-02-05 15:54:10
【问题描述】:

ASP.NET MVC4 - 第 7 章(Apres/Adam Freeman)第 175 页

有人完成了这个例子吗?

我正在尝试做书中的示例,是准备数据库部分下的部分。我面临的问题是视图没有列出任何产品,我的表产品有一些行但视图没有显示任何内容。即使我注释掉了连接字符串,它也没有显示任何错误。

我的产品类(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; }
    }
}

忍者工厂

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

        }
    }
}

产品仓库

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

【问题讨论】:

    标签: asp.net-mvc-4 entity-framework-6


    【解决方案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"/>
    

    您可以通过右键单击所需的数据库并选择属性来获取连接路径,它将是标记为连接字符串的那个。

    我希望这对正在阅读本书的其他人有所帮助!

    【讨论】:

      【解决方案2】:

      我自己造成了这个问题,因为我在创建 EFDbContext 类时忘记从 DbContext 继承。

      【讨论】:

        【解决方案3】:
        IProductRepository interface has IEnumerable<Product>Products{get;}
        

        【讨论】:

        • 虽然这可能会回答作者的问题,但它缺少一些解释性文字和/或文档链接。如果没有围绕它们的一些短语,原始代码 sn-ps 并不是很有帮助。您可能还会发现how to write a good answer 非常有帮助。请编辑您的答案 - From Review
        猜你喜欢
        • 2015-06-10
        • 1970-01-01
        • 1970-01-01
        • 2015-08-15
        • 1970-01-01
        • 1970-01-01
        • 2015-04-28
        • 2011-11-14
        • 1970-01-01
        相关资源
        最近更新 更多