views:

19

answers:

3
Patient.DivHospitalID (FK)
DivHospital.HospitalID (FK)
Hospital.HospitalID (PK)

I need to insert into DivHospital the Hospital and link/insert into Patient the DivHospital.

Patient tp = new Patient();
DivHospital dh = new DivHospital();

dh.HospitalReference.EntityKey =
     new EntityKey("transportPagerEntities.Hospital", "hospitalID", hospital);
         tp.DivHospitalReference.EntityKey = new  
         EntityKey("transportPagerEntities.DivHospital", "divHospitalID", hospitalref);

context.AddToDivHospital(dh);
context.AddToTransportPatient(tp);
context.SaveChanges();
+1  A: 

Assuming you aren't dealing with PKs (ints) and since you are using an ORM you shouldn't be.

You don't need to do the EntityKey stuff, just set them directly.

Patient tp = new Patient();
DivHospital dh = new DivHospital();

dh.Hospital = hospital;
tp.DivHospital = hospitalref;

context.AddToDivHospital(dh);
context.AddToTransportPatient(tp);

context.SaveChanges();
Nix
I am dealing with PKs.Patient.DivHospitalID (FK) => DivHospital.DivHospitalID (PK)DivHospital.HospitalID (FK) => Hospital.HospitalID (PK)I need to create the DivHospital "record" and then link that with Patient.
Ian
A: 

I think this is more of a problem with the DB Design and a useless table.

Ian
A: 

With EntityFramework this is really easy (if I understand your problem):

Patient tp = new Patient();
DivHospital dh = new DivHospital();

dh.Patient.Add(tp);  //magic

context.AddToDivHospital(dh);
context.SaveChanges();
itchi