我正在尝试将一组人插入到数据库表中。我可以毫无问题地从数据库中进行选择,但是当我尝试输入时,我得到了
Cannot add an entity with a key that is already in use
我知道这意味着什么,但为什么会这样。我在表 id 上有增量。这是我的代码。
public static List<Person> GetPeople(DataContext db)
{
List<Person> people = new List<Person>();
Table<Person> People = db.GetTable<Person>();
for (int i = 0; i < 5; i++)
{
Person pers = new Person();
pers.PersFirstName = "Wesley " + i;
//pers.PersId is the primary key that I want to auto-increment and not have to set it here
people.Add(pers);
}
People.InsertAllOnSubmit(people);
People.Context.SubmitChanges();
IQueryable<Person> query =
from person in People
select person;
return people;
}
问题发生在生产线上
People.InsertAllOnSubmit(people);
有什么特殊的方法我必须用 Linq 设置自动增量吗?