I have 2 tables in SQL. One is called Training
and one is called Consultants
. Training is couple of fields like ID, Place, Time, Date
and it also has ConsultantName
which is foreign key and is connected with Consultants
having ConsultantName, ConsultantFirstName etc.
During save of Training i save the training first and then pass the Training value as varTraining. I tried to attatch it to context as per some suggestions I found on stack but it doesn't seem to work:
Training Training = context.Trainings.First(p => p.TrainingID == varTraining.TrainingID);
context.Trainings.Attach(Training);
currentUczestnik.Training = Training;
Here's the code:
using (var context = new EntityBazaCRM(Settings.sqlDataConnectionDetailsCRM))
{
Training Training = context.Trainings.First(p => p.TrainingID == varTraining.TrainingID);
foreach (var currentUczestnik in listaUczestnikow)
{
context.Trainings.Attach(Training);
currentUczestnik.Training = Training;
if (context.TrainingUczestnicies.Any(t => t.TrainingUczestnicy1 == currentUczestnik.TrainingUczestnicy1))
{
context.TrainingUczestnicies.Attach(currentUczestnik);
context.ObjectStateManager.ChangeObjectState(currentUczestnik, EntityState.Modified);
}
else
{
// context.Detach(currentUczestnik.Consultant);
context.Consultants.Attach(currentUczestnik.Consultant);
context.TrainingUczestnicies.AddObject(currentUczestnik);
//context.TrainingUczestnicies.Attach(
}
}
try
{
context.SaveChanges();
}
catch (Exception ex)
{
string excepion = ex.InnerException.ToString();
MessageBox.Show(excepion);
}
}
Edit:
The full story looks like this.
There is this Table with Consultants. People are loaded with:
using (var context = new EntityBazaCRM(Settings.sqlDataConnectionDetailsCRM)) {
IQueryable<Konsultanci> listaKonsultantow = from k in context.Konsultancis select k;
objectListViewKonsultanci.SetObjects(listaKonsultantow);
}
They are loaded into ObjectListview. Then when user clicks on that ObjectListView the following Consultants are "copied" into ObjectListView Participants.
foreach (Konsultanci konsultant in objectListViewKonsultanci.SelectedObjects) {
dodajUczestnikowSzkolen(konsultant);
}
New object is created called SzkolenieUczestniczy (Participants) and Consultant is assigned to it as it's connected. Some more properties are added too.
private void dodajUczestnikowSzkolen(Konsultanci konsultant) {
SzkolenieUczestnicy nowyUczestnik = new SzkolenieUczestnicy();
nowyUczestnik.Konsultanci = konsultant;
//nowyUczestnik.SzkolenieUzytkownik = Settings.currentlyLoggedKonsultant.KonsultantNazwa;
nowyUczestnik.SzkolenieUzytkownikData = DateTime.Now;
//listaUczestnicy.Add(nowyUczestnik);
objectListViewUczestnicy.AddObject(nowyUczestnik);
}
Now users saves Training (some fields like Date, Time, Topic were attached to to currentSzkolenie (Training):
private void sqlZapiszSzkolenie() {
using (var context = new EntityBazaCRM(Settings.sqlDataConnectionDetailsCRM)) {
if (context.Szkolenies.Any(t => t.SzkolenieID == currentSzkolenie.SzkolenieID)) {
context.Szkolenies.Attach(currentSzkolenie);
context.ObjectStateManager.ChangeObjectState(currentSzkolenie, EntityState.Modified);
} else {
context.Szkolenies.AddObject(currentSzkolenie);
}
context.SaveChanges();
context.Detach(currentSzkolenie);
}
}
So after I saved Training and the Training is now in database I head off to save Participants. I take currentSzkolenie
(global var for Training) and pass it as variable:
private void sqlZapiszUczestnikow(ObjectListView listaUczestnikow, Szkolenie varSzkolenie) {
foreach (SzkolenieUczestnicy currentUczestnik in listaUczestnikow.Objects) {
using (var context = new EntityBazaCRM(Settings.sqlDataConnectionDetailsCRM)) {
Szkolenie szkolenie = context.Szkolenies.First(p => p.SzkolenieID == varSzkolenie.SzkolenieID);
context.Attach(szkolenie);
currentUczestnik.Szkolenie = szkolenie;
// currentUczestnik.Szkolenie.EntityKey = szkolenie.EntityKey;
if (context.SzkolenieUczestnicies.Any(t => t.SzkolenieUczestnicy1 == currentUczestnik.SzkolenieUczestnicy1)) {
context.SzkolenieUczestnicies.Attach(currentUczestnik);
context.ObjectStateManager.ChangeObjectState(currentUczestnik, EntityState.Modified);
} else {
// context.Detach(currentUczestnik.Konsultanci);
context.Konsultancis.Attach(currentUczestnik.Konsultanci);
context.SzkolenieUczestnicies.AddObject(currentUczestnik);
//context.SzkolenieUczestnicies.Attach(
}
try {
context.SaveChanges();
} catch (Exception ex) {
string excepion = ex.InnerException.ToString();
MessageBox.Show(excepion);
}
}
}
}
And that's about it.. it throws error on currentUczestnik.Szkolenie = szkolenie;
See Question&Answers more detail:
os