Insert into multiple tables ADO .NET Entity framework

  • Thread starter Thread starter everplano
  • Start date Start date
E

everplano

Hi,
I am using the entity framework to access the database. I have create
a data service, the proxy, and I am working on the business service.
So far simple cases have worked fine, but when tring to insert into
multiple tables, I can not get the insert into the child table. Here
is a bunch of scenarios I have tried, they are not all, but this
illustrates how much effort I have put into this. Can somebody come up
with ideas how to solve this, or what I might be doing wrong? Thanks a
lot. Enrique

Uri uri = new
Uri(ConfigurationManager.AppSettings["MyDataServicesUri"]);
ODSEntities tx = new MyEntities(uri);
tx.MergeOption = MergeOption.AppendOnly;

par_obj po = new par_obj();
po.addr_1 = "123 main";
...
po.cust_nm = "Godd Cust";
tx.AddObject("par_obj", po);
tx.SaveChanges();

Case 1:
child_obj co = new child_obj();
co.mnrty_cert_dtm = 1234;
....
co.dbl_inv_ind = 9999;
po.child_obj.Add(co);
tx.UpdateObject(po);

tx.SaveChangesDefaultOptions = SaveChangesOptions.None;
tx.SaveChanges();

This goes through, but doesn't create the child record

Case 2:
child_obj co = new child_obj();
co.mnrty_cert_dtm = 1234;
....
co.dbl_inv_ind = 9999;
po.child_obj.Add(co);
tx.AddObject("child_obj", co);

tx.SaveChanges();

Throws exception: Entities in 'MyEntities.child_obj' participate in
the 'FK_child_obj_cd_par_obj' relationship. 0 related 'par_obj' were
found. 1 'par_obj' is expected

Case 3:
child_obj co = new child_obj();
co.par_obj = tx.par_obj.Where(vco_vm => vco_vm.vnd_nbr ==
vnd.VendorMtr.vnd_nbr && vco_vm.div_nbr ==
vnd.VendorMtr.div_nbr).First();
co.mnrty_cert_dtm = 1234;
...
co.dbl_inv_ind = 9999;
tx.AddTochild_obj(co);
tx.UpdateObject(po);

tx.SaveChanges();
Throws same error

Case 4:
I create a new parent object, selecting the data from the DB, which
works fine:

var newVM = (from no in tx.par_obj
where no.po_nbr == 123 && no.div_nbr == 456
select no).First();

child_obj co = new child_obj();
co.po_id = newVM.po_id;
...
co.dbl_inv_ind = 9999;
co.par_obj = newVM;
tx.UpdateObject(newVM);
tx.SaveChanges();

This goes through, but doesn't create the child record

Case 5:
Same as before, except that instead of assigning the parent object, I
add the child to the parent:
newVM.child_obj.Add(co);
tx.UpdateObject(newVM);
tx.SaveChanges();

Same result as before

Case 6:
var newVM = (from vm in tx.par_obj
where vm.vnd_nbr == 123 && vm.div_nbr == 456
select vm).First();

child_obj co = new child_obj();
co.po_id = newVM.po_id;
...
co.dbl_inv_ind = 9999;
tx.AddObject("child_obj", co);
tx.SaveChanges();

Throws same exception: Entities in 'MyEntities.child_obj' participate
in the 'FK_child_obj_cd_par_obj' relationship. 0 related 'par_obj'
were found. 1 'par_obj' is expected
 
There's more activity on the forums for the Entity Framework than
there is in the newsgroups. I'd try re-posting your question on
http://forums.microsoft.com/MSDN/ShowForum.aspx?siteid=1&ForumID=533.

--Mary

Hi,
I am using the entity framework to access the database. I have create
a data service, the proxy, and I am working on the business service.
So far simple cases have worked fine, but when tring to insert into
multiple tables, I can not get the insert into the child table. Here
is a bunch of scenarios I have tried, they are not all, but this
illustrates how much effort I have put into this. Can somebody come up
with ideas how to solve this, or what I might be doing wrong? Thanks a
lot. Enrique

Uri uri = new
Uri(ConfigurationManager.AppSettings["MyDataServicesUri"]);
ODSEntities tx = new MyEntities(uri);
tx.MergeOption = MergeOption.AppendOnly;

par_obj po = new par_obj();
po.addr_1 = "123 main";
...
po.cust_nm = "Godd Cust";
tx.AddObject("par_obj", po);
tx.SaveChanges();

Case 1:
child_obj co = new child_obj();
co.mnrty_cert_dtm = 1234;
....
co.dbl_inv_ind = 9999;
po.child_obj.Add(co);
tx.UpdateObject(po);

tx.SaveChangesDefaultOptions = SaveChangesOptions.None;
tx.SaveChanges();

This goes through, but doesn't create the child record

Case 2:
child_obj co = new child_obj();
co.mnrty_cert_dtm = 1234;
....
co.dbl_inv_ind = 9999;
po.child_obj.Add(co);
tx.AddObject("child_obj", co);

tx.SaveChanges();

Throws exception: Entities in 'MyEntities.child_obj' participate in
the 'FK_child_obj_cd_par_obj' relationship. 0 related 'par_obj' were
found. 1 'par_obj' is expected

Case 3:
child_obj co = new child_obj();
co.par_obj = tx.par_obj.Where(vco_vm => vco_vm.vnd_nbr ==
vnd.VendorMtr.vnd_nbr && vco_vm.div_nbr ==
vnd.VendorMtr.div_nbr).First();
co.mnrty_cert_dtm = 1234;
...
co.dbl_inv_ind = 9999;
tx.AddTochild_obj(co);
tx.UpdateObject(po);

tx.SaveChanges();
Throws same error

Case 4:
I create a new parent object, selecting the data from the DB, which
works fine:

var newVM = (from no in tx.par_obj
where no.po_nbr == 123 && no.div_nbr == 456
select no).First();

child_obj co = new child_obj();
co.po_id = newVM.po_id;
...
co.dbl_inv_ind = 9999;
co.par_obj = newVM;
tx.UpdateObject(newVM);
tx.SaveChanges();

This goes through, but doesn't create the child record

Case 5:
Same as before, except that instead of assigning the parent object, I
add the child to the parent:
newVM.child_obj.Add(co);
tx.UpdateObject(newVM);
tx.SaveChanges();

Same result as before

Case 6:
var newVM = (from vm in tx.par_obj
where vm.vnd_nbr == 123 && vm.div_nbr == 456
select vm).First();

child_obj co = new child_obj();
co.po_id = newVM.po_id;
...
co.dbl_inv_ind = 9999;
tx.AddObject("child_obj", co);
tx.SaveChanges();

Throws same exception: Entities in 'MyEntities.child_obj' participate
in the 'FK_child_obj_cd_par_obj' relationship. 0 related 'par_obj'
were found. 1 'par_obj' is expected
 
Back
Top