无法确定类型的复合主键排序

时间:2017-03-16 03:33:30

标签: c# entity-framework asp.net-mvc-5

  

{"无法确定类型的复合主键排序   ' Conference_Project.Models.Login&#39 ;.使用ColumnAttribute(请参阅   http://go.microsoft.com/fwlink/?LinkId=386388)或HasKey方法   (见http://go.microsoft.com/fwlink/?LinkId=386387)指定一个   订购复合主键。"}

 [Table("ConferenceLogin")]
public class Login
{

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long confid { get; set; }
    [Key]
    public string emailID { get; set; }       
    [Key]
    public string registration { get; set; }
    [Key]
    public long regNo { get; set; }        
}

enter image description here

Version Used : EntityFramework.6.1.3  And MVC5

我想要这个(emailIDregistrationregNo)的唯一价值 将all设置为主键,然后EntityFramework显示错误

如何在EntityFramework中使用多个主键?

2 个答案:

答案 0 :(得分:6)

虽然我不确定发生此错误的基础逻辑,但我确实对我正在上课的项目有同样的问题。对我有用的是将一个Column(order)装饰器添加到Key标签转动

[Key] 

进入

[Key, Column(Order = n)]

其中n是相关密钥的从0开始的索引。

有鉴于此,您的课程应如下所示:

[Table("ConferenceLogin")]
public class Login
{
    [Key, Column(Order = 0), DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public long confid { get; set; }
    [Key, Column(Order = 1)]
    public string emailID { get; set; }       
    [Key, Column(Order = 2)]
    public string registration { get; set; }
    [Key, Column(Order = 3)]
    public long regNo { get; set; }        
}

希望这对你有用,对我有用!

答案 1 :(得分:0)

要将注册列设置为主键,您必须设置数据类型nvarchar(50)。 nvarchar(128)不允许你使它成为主键