多主键与ASP .NET MVC 3 [英] Multiple Primary Key with asp .net mvc 3

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

问题描述

我使用的ASP.NET MVC 3,和我有一个包含2主键的实体问题,当我尝试在表中插入数据。

I'm using asp .net mvc 3, and I have a problem with an entity that contains 2 primary key, when I try to insert data in the table.

 public class LineItem
    {
        [Key]
        public int OrderId { get; set;}
        [Key]
        public int LineNum  { get; set;}
        public string ItemId { get; set;}
        public int Quantity { get; set;}
        public decimal UnitPrice { get; set; }

    }

当我尝试插入我得到这个错误:

when I try to insert I got this error :

无法确定复合主
  类型的键排序
  ApplicationMVC3.Models.LineItem。使用
  ColumnAttribute或HasKey方法
  指定复合订单
  主键。

Unable to determine composite primary key ordering for type 'ApplicationMVC3.Models.LineItem'. Use the ColumnAttribute or the HasKey method to specify an order for composite primary keys.

可能有人帮我请!

推荐答案

假设这实际上是一个复合键,因为你不能有2个主键...错误消息告诉你该怎么做,即增加一个订购。您可以通过添加 [列(订单= 0)] [列(订单= 1)] 来做到这一点您的键列。

Assuming this is actually a composite key, since you can't have 2 primary keys... The error message tells you exactly what to do, namely add an order. You can do this by adding [Column(Order = 0)] and [Column(Order = 1)] to your key columns.

有关您的例子:

public class LineItem
    {
        [Key][Column(Order = 0)]
        public int OrderId { get; set;}
        [Key][Column(Order = 1)]
        public int LineNum  { get; set;}
        public string ItemId { get; set;}
        public int Quantity { get; set;}
        public decimal UnitPrice { get; set; }

    }

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

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