ASP.NET MVC 3的EntityType没有定义键 [英] ASP.NET MVC 3 EntityType has no key defined

查看:769
本文介绍了ASP.NET MVC 3的EntityType没有定义键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要显示客户信息。
然后,我创建了一些班;客户,送货,订购,订单行,产品和rentalDB。
rentalDB类设置5 DbSet产品,客户,订单,订单行和交货。
当我做UserController中以列表视图,无法显示客户信息,这需要错误:

 模型生成过程中检测到一个或多个验证错误:
System.Data.Edm.EdmEntityType:的EntityType订单行没有定义键。定义此的EntityType的关键。
System.Data.Edm.EdmEntityType:的EntityType交货没有定义键。定义此的EntityType的关键。
System.Data.Edm.EdmEntitySet:的EntityType:EntitySet的订单行是基于一个没有定义的键类型订单行。
System.Data.Edm.EdmEntitySet:的EntityType:EntitySet的交付是基于一个没有定义的键类型交货。

我不知道为什么这些企业需要的关键?
我不知道这个错误..
你能帮助我吗?

- UserController.cs -

 命名空间MvcApplication2.Controllers
{
公共类UserController的:控制器
  {
    //
    // GET:/用户/
    rentalDB _db =新rentalDB();    公众的ActionResult指数()
    {
        VAR模型= _db.Customer;
        返回查看(模型);
    }
  }
}

- 在Models文件夹Delivery.cs -

 命名空间MvcApplication2.Models
{
  公共类交货
  {
    公众诠释的TrackID {搞定;组; }
    公共字符串的地址{搞定;组; }
    公共字符串后code {搞定;组; }
    公共小数deliveryPrice {搞定;组; }
    公众的DateTime deliveryDate {搞定;组; }
    公众的DateTime returnDate {搞定;组; }
  }
}

- 在Models文件夹OrderLine.cs -

 命名空间MvcApplication2.Models
{
   公共类订单行
   {
    公众诠释basketId {搞定;组; }
    公众诠释的productId {搞定;组; }
    公众诠释数量{搞定;组; }
   }
}


解决方案

为了使用实体框架,每一个实体需求的关键。这是EF如何跟踪对象在其缓存,帖子更新回底层数据存储和链接相关对象在一起。

此致对象已经有钥匙,你只需要告诉他们EF:

 命名空间MvcApplication2.Models
{
  公共类交货
  {
    [关键词]公众诠释的TrackID {搞定;组; }
    公共字符串的地址{搞定;组; }
    公共字符串后code {搞定;组; }
    公共小数deliveryPrice {搞定;组; }
    公众的DateTime deliveryDate {搞定;组; }
    公众的DateTime returnDate {搞定;组; }
  }
}

I want to display customer information. Then I created some classes; Customer, Delivery, Order, OrderLine, Product, and rentalDB. rentalDB class sets 5 DbSet of Product, Customer, Order, OrderLine, and Delivery. When I make UserController with list view, I cannot display the customer information, and it takes errors:

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.

I don't know why these entities require key? I have no idea for this error.. Could you help me?

--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 in 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 in Models folder--

namespace MvcApplication2.Models
{
   public class OrderLine
   {
    public int basketId { get; set; }
    public int productId { get; set; }
    public int quantity { get; set; }
   }
}

解决方案

In order to use the entity framework, every entity needs a key. This is how EF tracks objects in its cache, posts updates back to the underlying data store, and links related objects together.

Yours objects already have keys, you just need to tell the EF about them:

namespace MvcApplication2.Models
{
  public class Delivery
  {
    [Key] 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; }
  }
}

这篇关于ASP.NET MVC 3的EntityType没有定义键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆