-4

I have added the patient and visit tables in reg_visit table in Entity Framework 6.1 code-first but when run the below code, I get this error:

Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.

Code:

Models.VisitDoctotDb oVisitdb = new Models.VisitDoctotDb();
Models.RegVisit oReg_visit = new Models.RegVisit();

oReg_visit.Patient = oPatient;
oReg_visit.Visit = oVisit;

oVisitdb.Reg_visit.Add(Reg_visit); ===> error
oVisitdb.SaveChanges();
marc_s
  • 704,970
  • 168
  • 1,303
  • 1,425
Ali Naami
  • 1
  • 2
  • 4
    Show EntityValidationErrors... – Ilya Dec 13 '14 at 10:04
  • add the try catch in this [answer](http://stackoverflow.com/a/7798264/1498624) to your code, go from there and if you can't solve it add the output to the question so we could help you better – grabthefish Dec 13 '14 at 11:01

2 Answers2

0

if (oPatient) was already added to the database, you mustn't add oReg_visit to DB because it is already attached.

Models.VisitDoctotDb oVisitdb = new Models.VisitDoctotDb();
Models.RegVisit oReg_visit = new Models.RegVisit();

oReg_visit.Patient = oPatient;
oReg_visit.Visit = oVisit;


oVisitdb.SaveChanges();

this code should save your object to database

Anas Jame
  • 202
  • 2
  • 8
0

If you use Entity Framework you can have a look at my answer on Solution for “Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. Hope this helps...

Community
  • 1
  • 1
Murat Yıldız
  • 10,459
  • 6
  • 58
  • 60