I am now working on a MVC project on running the program I've got an error on trying to retrieve the data from my SqlServer Database.
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);
}
}
}
Models: I have two classes(Products.cs and ProductContext.cs)
For 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; }
}
}
For 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);
}
}
}
I hope someone can help me. I am only a beginner in MVC. Thank you
Try to modify your Products class like this - Adding Key attribute for ProductKey
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; }
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.