在数据迁移时报以下错误:System.InvalidOperationException: Saving or accepting changes failed because more than one entity of type 'LinkInfo' have the same primary key value. Ensure that explicitly set primary key values are unique. Ensure that database-generated primary keys are configured correctly in the database and in the Entity Framework model. Use the Entity Designer for Database First/Model First configuration. Use the 'HasDatabaseGeneratedOption" fluent API or 'DatabaseGeneratedAttribute' for Code First configuration.
如果不添加数据就不报错,在SEED方法中添加数据就会报错,很奇怪,有人遇到过此类问题吗,求解。
protected override void Seed(TJiTa.Service.DataContext context) {
context.Links.AddOrUpdate(
p => p.LinkText,
new LinkInfo { LinkText = "搜狐视频", LinkUrl = "http://www.sohu.com/" },
new LinkInfo { LinkText = "test", LinkUrl = "http://www.test.com/" }
);
}
你不认识英文吗,LinkInfo主键值重复
public class LinkInfo {
public LinkInfo() {
this.ID = 0;
this.LinkText = string.Empty;
this.LinkUrl = string.Empty;
this.Creation = DateTime.Now;
}
[Key]
public int ID { get; set; }
public string LinkText { get; set; }
public string LinkUrl { get; set; }
public DateTime Creation { get; set; }
}
这是实体,只有一个主键,且新建表的主键都是自增长型的。
已解决。