c# - “System.Data.Entity.ModelConfiguration.ModelValidationException”出现在 EntityFramework.dll 中,但未在用户代码中处理

标签 c# sql-server asp.net-mvc razor visual-studio-2013

我现在正在处理一个运行该程序的 MVC 项目,我在尝试从我的 SqlServer 数据库中检索数据时遇到错误。

Controller :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Activity.Models;

namespace Activity.Controllers
{
     public class ProductsController : Controller
    {
        public ActionResult ProductDetails(int id)
        {
            ProductContext productsContext = new ProductContext();
            Products prod = productsContext.Product.Single(pru => pru.ProductKey == id);

            return View(prod);
        }

    }
}

模型:我有两个类(Products.cs 和 ProductContext.cs)

对于 ProductContext.cs

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace Activity.Models
{
    public class ProductContext : DbContext
    {
        public DbSet<Products> Product { get; set; }
    }
}

对于 Products.cs

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace Activity.Models
{

    [Table("PruProductDetails")]
    public class Products
    {
        public int ProductKey { get; set; }
        public string ProductCode { get; set; }
        public string ProductName { get; set; }
        public double Price { get; set; }
        public string Discountable { get; set; }
        public string DateAdded { get; set; }
        public double Discount { get; set; }
        public int Quantity { get; set; }
        public int ReOrderLevel { get; set; }
        public int OrderLimit { get; set; }
    }
}

Web.Config

<connectionStrings>
    <add name="ProductContext"
         connectionString="server=(local); database=Exam; integrated security=SSPI"
          providerName="System.Data.SqlClient"/>
   </connectionStrings>

Global.asax

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;

namespace Activity
{

    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            Database.SetInitializer<Activity.Models.ProductContext>(null);
            AreaRegistration.RegisterAllAreas();
            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
    }
 }

我希望有人能帮助我。我只是 MVC 的初学者。谢谢

最佳答案

尝试像这样修改您的 Products 类 - 为 ProductKey 添加 Key 属性

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;

namespace Activity.Models
{

[Table("PruProductDetails")]
public class Products
{
    [Key]
    public int ProductKey { get; set; }
    public string ProductCode { get; set; }
    public string ProductName { get; set; }
    public double Price { get; set; }
    public string Discountable { get; set; }
    public string DateAdded { get; set; }
    public double Discount { get; set; }
    public int Quantity { get; set; }
    public int ReOrderLevel { get; set; }
    public int OrderLimit { get; set; }
}

关于c# - “System.Data.Entity.ModelConfiguration.ModelValidationException”出现在 EntityFramework.dll 中,但未在用户代码中处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38298906/

相关文章:

c# - 当我尝试向项目添加内容时,Visual Studio 崩溃了

c# - ASP.NET VNext 调试 MVC 源代码

c# - 实体 IEnumerable 仅获取基类 - 而不是派生类

c# - C# 对象引用如何在内存中/在运行时(在 CLR 中)表示?

c# - 字段的 ASP.NET MVC 内联帮助程序

java - C# 和 Java 将 DateTime 转换为字节数组时的区别

sql - 使用 GROUP BY 多列的查询

sql - 如何在sqllocaldb中添加用户名和密码

sql - 查询根据列名查找表的行数?

c# - Entity Framework 无法保存 - Datetime2 到 DateTime 的转换失败