데이터베이스 테이블에 사람 그룹을 삽입하려고합니다. 나는 문제없이 데이터베이스에서 선택할 수 있지만 입력 할 때 나는 이것이 무엇을 의미하는지 알고 난linq을 사용하여 데이터베이스에 삽입 할 수 없습니다.
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에와 자동 증가를 설정해야합니다 몇 가지 특별한 방법이 있나요에서 어떻게됩니까? 당신의 DBML 디자이너에서
데이터베이스에서 ID 값이 증가하도록 설정해야합니다. 'PersonId'가 기본 키라고 설정 했습니까? – Kai
그래, 벌써 했어 –
매핑을 생성하기 전에 pk를 설정 했습니까? 매핑 파일을 다시 만들지 않습니다. – Kai