首页 新闻 赞助 找找看

ef 数据迁移问题 database-generated primary keys

0
悬赏园豆:50 [待解决问题]

在数据迁移时报以下错误: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/" }
    );
}

老S的主页 老S | 初学一级 | 园豆:11
提问于:2016-04-13 18:59
< >
分享
所有回答(2)
0

你不认识英文吗,LinkInfo主键值重复

上帝之城 | 园豆:2549 (老鸟四级) | 2016-04-13 20:22

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

 

这是实体,只有一个主键,且新建表的主键都是自增长型的。

支持(0) 反对(0) 老S | 园豆:11 (初学一级) | 2016-04-14 06:12
0

已解决。

老S | 园豆:11 (初学一级) | 2016-04-14 06:21
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册