insert value to foreign key

2019-08-03 01:40发布

问题:

problem is with foreign key:

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_uzytkownik_Logowanie". The conflict occurred in database "Restauracja", table "dbo.Logowanie", column 'LoginID'.
The statement has been terminated.

I check this using breakpoints, and primary key in Logowanie table was added when breakpoints (running application) was after

baza.SubmitChanges();

Primary key of LoginID in logowanie table is added automatically during SubmitChanges.

How to copy value of LoginID from logowanie table to LoginID in uztkownik table? I add foreign key value here, but here LoginID hasn't value yet.

Logowanie newlog = new Logowanie()
{
   Login = model.LoginModel.Użytkownik,
   Haslo = model.LoginModel.Hasło,
   konto = model.LoginModel.Konto
};

uzytkownik user = new uzytkownik()
{
   imie = model.uzytkownikModle.imie,
   nazwisko = model.uzytkownikModle.nazwisko,
   pesel = model.uzytkownikModle.pesel,
   nip = model.uzytkownikModle.nip,
   telefon = model.uzytkownikModle.telefon,
   adres_zamieszkania = model.uzytkownikModle.adres_zamieszkania,
   email = model.uzytkownikModle.email,
   LoginID = newlog.LoginID //<<<----------------
};

baza.Logowanies.InsertOnSubmit(newlog);
baza.uzytkowniks.InsertOnSubmit(user);

baza.SubmitChanges();

回答1:

uzytkownik should have a property called Logowanie, which you can set to the new instance you created but didn't Submit yet:

user.Logowanie = newlog;


回答2:

Since it's a foreign key relationship, your Logowanie class should contain a property for IQueryable<uzytkownik> that is used to relate the objects properly in a one-to-many manner. As a result, there is no longer a need to assign the foreign key manually, it can be done through the object heirarchy:

Logowanie newlog = new Logowanie()
{
   Login = model.LoginModel.Użytkownik,
   Haslo = model.LoginModel.Hasło,
   konto = model.LoginModel.Konto
};

uzytkownik user = new uzytkownik()
{
   imie = model.uzytkownikModle.imie,
   nazwisko = model.uzytkownikModle.nazwisko,
   pesel = model.uzytkownikModle.pesel,
   nip = model.uzytkownikModle.nip,
   telefon = model.uzytkownikModle.telefon,
   adres_zamieszkania = model.uzytkownikModle.adres_zamieszkania,
   email = model.uzytkownikModle.email,
   // Remove this line entirely   LoginID = newlog.LoginID //<<<----------------
};

// Add the child object (user) to your Logowanie object (newlog)
newlog.uzytkowniks.Add(user)  // This may just be uzytkownik. Pluralization in LINQ is weird

// It is not necessary to queue the user object for insertion
// the newlog object will do that for you
baza.Logowanies.InsertOnSubmit(newlog);
// baza.uzytkowniks.InsertOnSubmit(user);

// SubmitChanges at this point will insert all of the children
// of newlog and assign the IDs properly
baza.SubmitChanges();

// at this point you should be able to get the newlog.LoginID
// and the user.UserID (assuming this is what you called it)

When I first ran across this it seemed a little backward to me, but once you break the relationships down into their class representations it really starts to make sense. It's also fantastic that it does all of the relationship assignments for you.