Tôi muốn hiển thị thông tin khách hàng. Sau đó, tôi đã tạo một số lớp học; Khách hàng, Phân phối, Đơn đặt hàng, Đơn hàng, Sản phẩm và cho thuêDB. lớp cho thuêDBDB đặt 5 DbSet sản phẩm, khách hàng, đơn đặt hàng, đơn đặt hàng và giao hàng. Khi tôi làm UserController với xem danh sách, tôi không thể hiển thị thông tin khách hàng, và phải mất lỗi:Loại thực thể ASP.NET MVC 3 không có khóa được xác định
One or more validation errors were detected during model generation:
System.Data.Edm.EdmEntityType: : EntityType 'OrderLine' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntityType: : EntityType 'Delivery' has no key defined. Define the key for this EntityType.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �OrderLine� is based on type �OrderLine� that has no keys defined.
System.Data.Edm.EdmEntitySet: EntityType: EntitySet �Delivery� is based on type �Delivery� that has no keys defined.
Tôi không biết lý do tại sao những thực thể đòi hỏi quan trọng? Tôi không biết gì về lỗi này .. Bạn có thể giúp tôi không?
--UserController.cs--
namespace MvcApplication2.Controllers
{
public class UserController : Controller
{
//
// GET: /User/
rentalDB _db = new rentalDB();
public ActionResult Index()
{
var model = _db.Customer;
return View(model);
}
}
}
--Delivery.cs trong Models folder--
namespace MvcApplication2.Models
{
public class Delivery
{
public int trackId { get; set; }
public String address { get; set; }
public String postCode { get; set; }
public decimal deliveryPrice { get; set; }
public DateTime deliveryDate { get; set; }
public DateTime returnDate { get; set; }
}
}
--OrderLine.cs trong Models folder--
namespace MvcApplication2.Models
{
public class OrderLine
{
public int basketId { get; set; }
public int productId { get; set; }
public int quantity { get; set; }
}
}
Điều này làm việc cho tôi trong trường hợp đặc biệt. Tôi đã thêm một bộ điều khiển sau khi viết mã cho mô hình và không nhận được lỗi. Tại thời điểm đó tôi nhận ra mô hình không phải là những gì tôi muốn có nghĩa là tất cả các quan điểm được tạo ra là rất sai vì vậy tôi đã xóa bộ điều khiển/khung nhìn. Sau đó, khi tôi sửa mô hình và thêm bộ điều khiển, lỗi xuất hiện. Sau khi rối tung khoảng một giờ, tôi đã thêm [Key] và nó đã hoạt động. Có lẽ visual studio Mvc weirdness hay cache, không biết ... – isitdanny