【问题标题】:Unable to determine composite primary key ordering for type无法确定类型的复合主键排序
【发布时间】:2017-08-07 01:57:06
【问题描述】:

{"无法确定类型的复合主键顺序 'Conference_Project.Models.Login'。使用 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; }        
}

Version Used : EntityFramework.6.1.3  And MVC5

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

EntityFramework如何使用多个主键?

【问题讨论】:

  • 如何在EntityFramework中使用多个主键?点击阅读异常信息中的链接。
  • 您是否要使用 Code First 在表之间创建 n:n(多对多)关系(显示与其相关的所有表)?通常HasKeyCreateModel() 之前这样使用:builder.Entity<ForeignTablePropertyName>().HasKey(p => new { p.emailID, p.registration, p.regNo });
  • 解决方案已在我的错误链接中可用,只需设置 [Column(Order = 1)] 订单号

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


【解决方案1】:

虽然我不确定为什么会发生此错误的基本逻辑,但我在为课堂工作的项目中确实遇到了同样的问题。对我有用的是在 Key 标签中添加一个 Column(order) 装饰器

[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; }        
}

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

【讨论】:

    【解决方案2】:

    要将 registration 列设置为主键,您必须设置数据类型 nvarchar(50),。 nvarchar(128) 不允许您将其设为主键

    【讨论】:

      猜你喜欢
      • 2015-12-17
      • 2016-01-20
      • 2017-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多