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

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.