dimanche 17 avril 2016

Unable to add record to a table that having more than one relation with other tables

The code is written in ObjectContext. I've tried max to sort this.

tbluserprofiles.Id = (short)DataAccess.tblUserProfiles.NextID(db);

tbluserprofiles.Base.Active = true;
tbluserprofiles.Base.Status = clsSettings.Status.NEW;

tbluserprofiles.Base.CreatedBy = new Inventory365_MVC.Controllers.AccountController().LoginInfo.tbluser.Id;
tbluserprofiles.Base.ModifiedBy = new Inventory365_MVC.Controllers.AccountController().LoginInfo.tbluser.Id;
tbluserprofiles.Base.CreatedDate = DateTime.Now;
tbluserprofiles.Base.ModifiedDate = DateTime.Now;

//DataAccess.tblUserType tblUserTypes = db.tblUserTypes.Single(t => t.Id == tbluserprofiles.tblUserType.Id);
//DataAccess.tblCompanyProfile tblCompanyProfiles = db.tblCompanyProfiles.Single(t => t.Id == tbluserprofiles.tblCompanyProfile.Id);

db.AcceptAllChanges();
//tblUserTypes.tblUserProfiles.Add(tbluserprofiles);
//tblCompanyProfiles.tblUserProfiles.Add(tbluserprofiles);
//tbluserprofiles.tblCompanyProfile = tblCompanyProfiles;
//tbluserprofiles.tblUserTypes = tblUserTypes;
//db.tblUserProfiles.AddObject(tbluserprofiles);

db.tblUserTypes.Where("it.Id = @Id", new ObjectParameter("Id", tbluserprofiles.tblUserType.Id)).Execute(MergeOption.AppendOnly).First().tblUserProfiles.Add(tbluserprofiles);
db.tblCompanyProfiles.Where("it.Id = @Id", new ObjectParameter("Id", tbluserprofiles.tblCompanyProfile.Id)).Execute(MergeOption.AppendOnly).First().tblUserProfiles.Add(tbluserprofiles);

//db.AddTotblUserProfiles(tbluserprofiles);
//db.tblUserTypes.Single(t => t.Id == tbluserprofiles.tblUserTypes.Id).tblUserProfile.Add(tbluserprofiles);
//db.tblCompanyProfiles.Single(t => t.Id == tbluserprofiles.tblCompanyProfile.Id).tblUserProfiles.Add(tbluserprofiles);
//db.AcceptAllChanges();

db.SaveChanges();

Aucun commentaire:

Enregistrer un commentaire