简体   繁体   中英

insert value to foreign key

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();

uzytkownik应该具有一个名为Logowanie的属性,您可以将其设置为创建的新实例,但尚未提交:

user.Logowanie = newlog;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM