【问题标题】:'System.Data.Entity.ModelConfiguration.ModelValidationException' occurred in EntityFramework.dll but was not handled in user code“System.Data.Entity.ModelConfiguration.ModelValidationException”发生在 EntityFramework.dll 中,但未在用户代码中处理
【发布时间】:2016-07-11 03:52:23
【问题描述】:

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

控制器:

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 prod = productsContext.Product.Single(pru => pru.ProductKey == id);”我的控制器。
  • 请尝试在此链接中添加 try catch 建议,这样做会显示实际的异常消息,然后可以轻松解决问题。stackoverflow.com/questions/27777004/…
  • 还可以尝试为您的产品类的 ProductKey 属性添加 [Key] 属性,看看问题是否得到解决
  • {"在模型生成过程中检测到一个或多个验证错误:\r\n\r\nActivity.Models.Products: : EntityType 'Products' 没有定义键。定义此 EntityType 的键。\r\nProduct: EntityType: EntitySet 'Product' 基于没有定义键的类型'Products'。\r\n"}

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


【解决方案1】:

尝试像这样修改您的 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; }
}

}

【讨论】:

  • 您好,先生。我已经修好了。 [Key] 帮了大忙。谢谢:D
  • 只是好奇想知道,你取消标记我的答案了吗?
【解决方案2】:

我也有同样的错误解决方案非常简单,您的属性名称应该与 sql server 列和更重要的写作语法相匹配: 更重要的写作语法:

public ActionResult Details(int id) {
  EmployeeContext employeecontext = new EmployeeContext();
  Employee employee = employeecontext.Employees.Single(emp => emp.Employeeid == id);
  return View(employee);


  public ActionResult Details(int id)
        {
            EmployeeContext employeecontext = new EmployeeContext();
            Employee employee = employeecontext.Employees.Single(emp => emp.Employeeid == id);

            return View(employee);
        }

如果你写任何变量而不是int id,你必须编写int id,那么你会得到一个错误我希望我的这个错误的解决方案应该会有所帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    相关资源
    最近更新 更多