类型' System.Data.Entity.ModelValidationException'的例外情况发生在EntityFramework.dll中但未在用户代码中处理

时间:2017-12-06 07:21:16

标签: asp.net-mvc-5

我正在学习Pro Asp.net MVC5'的书。我跟随本书中的例子。

从Mock到localDb的绑定数据发生错误,这是第7.4.5章的例子

崩溃截图在这里。 enter image description here

向下滚动 enter image description here 细节是

' {"在模型生成期间检测到一个或多个验证错误:\ r \ n \ r \ nSportsStore.Domain.Concrete.Product :: EntityType'产品'没有定义键。定义此EntityType的键。\ r \ nProducts:EntityType:EntitySet' Products'是基于类型'产品'没有定义键。\ r \ n"}'

private void AddBindings()
    {
        //Mock<IProductsRepository> mock = new Mock<IProductsRepository>();
        //mock.Setup(m => m.Products).Returns(new List<Product>{
        //    new Product { Name = "Football", Price = 25 },
        //    new Product { Name = "Surf board", Price = 179 },
        //    new Product { Name = "Running shoes", Price = 95 }
        //    });
        //kernel.Bind<IProductsRepository>().ToConstant(mock.Object);
        kernel.Bind<IProductsRepository>().To<EFProductRepository>();
    }

我评论的代码运行良好,而未注释的代码将导致崩溃。 在List.cshtml上触发崩溃。第foreach(var p in Model)行 我知道这可能是localDb的问题,但我不知道如何调试并修复它。 Web.config中的connectionsString如下所示。

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

localDb截图。 enter image description here

VS DB设置截图 enter image description here

模型

public class Product
{
    public int ProjecID { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public string Category { get; set; }
}

Stack Trace enter image description here

0 个答案:

没有答案