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