我正在开发一个关于运行程序的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的初学者。谢谢
答案 0 :(得分: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; }
}
}
答案 1 :(得分:1)
我也有同样的错误解决方案非常简单,您的属性名称应与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的以下代码,否则会出现错误,我希望此错误的解决方案会有所帮助