У меня такой код:
class Importer {
private DBContext m_context;
public Importer() {
m_context = new DBContext();
}
public bool ImportPersons(List<MyPerson> p_Persons) {
foreach (MyPerson p in p_Persons) {
if (!import(p)) return false;
}
return true;
}
private bool import(MyPerson p_Person) {
address = new adr();
adr.city = p_Person.City;
adr.country = p_Person.Country;
adr.postal_code = p_Person.PostalCode;
try {
m_context.adresses.InsertOsSubmit(adr);
m_context.SubmitChanges();
} catch (Exception e) {
Console.WriteLine("Addr import error: {0}", e.Message);
return false;
}
person p = new person();
p.id_address = adr.id_addres;
p.name = p_Person.Name;
p.sname = p_Person.SName;
p.sex = p_Person.Sex;
try {
m_context.persons.InsertOnSubmit(p);
m_context.SubmitChanges();
} catch (Exception e) {
Console.WriteLine("Person import error: {0}", e.Message);
return false;
}
return true;
}
}
Когда я вызываю метод ImportPersons со списком из 20 человек, я получаю sicne 6-е (до конца) исключение: Невозможно добавить объект сключ, который уже используется во время вставки объекта person.
Alson, когда я начинаю с 6-го, исключение происходит из начала вставки.
Я должен добавить этот адрес.id_address столбец autoincremet в базе данных.
Можете ли вы помочь мне?