Hi, I have 2 tables with a one to many relationship. When I create the first object and save the change to the database, how can I retreive the primary key just created and use it as foreign key for the other object? here's the code:
using (BottinModel.BottinEntities db = new BottinModel.BottinEntities())
{
BottinModel.Entreprise entreprise = new BottinModel.Entreprise
{
nomEntreprise = TextBoxNomEntreprise.Text,
nom = TextBoxNom.Text,
prenom = TextBoxPrenom.Text,
adresse = TextBoxAdresse.Text,
ville = TextBoxVille.Text,
codePostal = TextBoxCodePostal.Text,
telephone = TextBoxTelephone.Text,
fax = TextBoxfax.Text,
siteWeb = TextBoxSiteWeb.Text,
};
foreach (ListItem listItem in ListeServicesOfferts.Items)
{
BottinModel.ServiceEntreprise serviceEntreprise = new BottinModel.ServiceEntreprise
{
pk_ServicesOffertsId = Convert.ToInt16(listItem.Value),
fk_entrepriseId = 2
};
db.ServiceEntreprises.AddObject(serviceEntreprise);
db.SaveChanges();
}
db.Entreprises.AddObject(entreprise);
int changes = db.SaveChanges();
}
}
how can I retreive the primary key just created and use it as foreign key for the other object?
If your primary key is auto generated,no need to fetch again,but just when you call SaveChanges() and add it into the primary table,you can directly get that through the newly-inserted instance。
Marked as answer by acheo on Feb 15, 2012 12:47 PM
acheo
Member
81 Points
67 Posts
How to retreive the primary key of the row just created?
Feb 13, 2012 05:01 PM|LINK
Hi, I have 2 tables with a one to many relationship. When I create the first object and save the change to the database, how can I retreive the primary key just created and use it as foreign key for the other object? here's the code:
using (BottinModel.BottinEntities db = new BottinModel.BottinEntities()) { BottinModel.Entreprise entreprise = new BottinModel.Entreprise { nomEntreprise = TextBoxNomEntreprise.Text, nom = TextBoxNom.Text, prenom = TextBoxPrenom.Text, adresse = TextBoxAdresse.Text, ville = TextBoxVille.Text, codePostal = TextBoxCodePostal.Text, telephone = TextBoxTelephone.Text, fax = TextBoxfax.Text, siteWeb = TextBoxSiteWeb.Text, }; foreach (ListItem listItem in ListeServicesOfferts.Items) { BottinModel.ServiceEntreprise serviceEntreprise = new BottinModel.ServiceEntreprise { pk_ServicesOffertsId = Convert.ToInt16(listItem.Value), fk_entrepriseId = 2 }; db.ServiceEntreprises.AddObject(serviceEntreprise); db.SaveChanges(); } db.Entreprises.AddObject(entreprise); int changes = db.SaveChanges(); } }Thanks!
Justin Dwyer
Participant
1119 Points
196 Posts
Re: How to retreive the primary key of the row just created?
Feb 13, 2012 05:29 PM|LINK
using (BottinModel.BottinEntities db = new BottinModel.BottinEntities()) { BottinModel.Entreprise entreprise = new BottinModel.Entreprise { nomEntreprise = TextBoxNomEntreprise.Text, nom = TextBoxNom.Text, prenom = TextBoxPrenom.Text, adresse = TextBoxAdresse.Text, ville = TextBoxVille.Text, codePostal = TextBoxCodePostal.Text, telephone = TextBoxTelephone.Text, fax = TextBoxfax.Text, siteWeb = TextBoxSiteWeb.Text, }; db.Entreprises.AddObject(entreprise); db.SaveChanges(); foreach (ListItem listItem in ListeServicesOfferts.Items) { BottinModel.ServiceEntreprise serviceEntreprise = new BottinModel.ServiceEntreprise { pk_ServicesOffertsId = Convert.ToInt16(listItem.Value), fk_entrepriseId = entreprise.id }; db.ServiceEntreprises.AddObject(serviceEntreprise); db.SaveChanges(); } } }Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: How to retreive the primary key of the row just created?
Feb 15, 2012 12:50 AM|LINK
If your primary key is auto generated,no need to fetch again,but just when you call SaveChanges() and add it into the primary table,you can directly get that through the newly-inserted instance。