一例Unable to determine composite primary key ordering for type错误的解决

Unable to determine composite primary key ordering for type 'Rztong.Entity.dtproperty'. Use the ColumnAttribute (see http://go.microsoft.com/fwlink/?LinkId=386388) or the HasKey method (see http://go.microsoft.com/fwlink/?LinkId=386387) to specify an order for composite primary keys.

出现错误的类:

public partial class dtproperty
    {
        [Key]
        [Column(Order = 0)]
        public int id { get; set; }
 
        public int? objectid { get; set; }
 
        [Key]
        [Column(Order = 1)]
        [StringLength(64)]
        public string property { get; set; }
 
        [StringLength(255)]
        public string value { get; set; }
 
        [StringLength(255)]
        public string uvalue { get; set; }
 
        [Column(TypeName = "image")]
        public byte[] lvalue { get; set; }
 
        public int version { get; set; }
    }

有id和property两个主键,但是都已经使用了column属性,但仍然报错。通过审查,引用的entity framework是6.0版本,没有问题。而且项目的目标框架也是4.5.2,也没有问题。

解决办法,在dtproperty类下面创建DtpropertyEntityConfiguration类:

class DtpropertyEntityConfiguration : EntityTypeConfiguration<dtproperty>
    {
        public DtpropertyEntityConfiguration()
        {
            HasKey(m => m.id);
            HasKey(m => m.property);
        }
    }

页面中要引用using System.Data.Entity.ModelConfiguration;

在Dbcontext继承类的OnModelCreating增加modelBuilder.Configurations.Add(new DtpropertyEntityConfiguration())配置

protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
   modelBuilder.Configurations.Add(new DtpropertyEntityConfiguration());
}

或者直接在Dbcontext继承类的OnModelCreating增加:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
    //使用HasKey设置组合主键
   modelBuilder.Entity<dtproperty>().HasKey(m => new { m.id,m.property});
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值