0

Noob here.

I'm struggling wrapping my head around a few things. It used to be pretty straightforward to grab some data from a web form and throw it in a database.

I have a view for patient that gathers information when registering the patient. The information lives across multiple tables.

To facilitate this, I've created the following in my patient model:

public class Patient
{
    //properties  
    public virtual PatientCondition patientcondition { get; set; }
    public virtual PatientInsurrance patientinsurance { get; set; }
}

This allows me to add fields to my view for each of those models. My view contains fields from both patient and patientinsurrance:

<div class="form-group">
    @Html.LabelFor(model => model.patientinsurance.InsuranceCompany, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">                                   
         @Html.EditorFor(model => model.patientinsurance.InsuranceCompany, new { htmlAttributes = new { @class = "form-control" } })
         @Html.ValidationMessageFor(model => model.patientinsurance.InsuranceCompany, "", new { @class = "text-danger" })
     </div>
</div>                     

<div class="form-group">
    @Html.LabelFor(model => model.EmergencyContactPhoneNumber, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.EmergencyContactPhoneNumber, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.EmergencyContactPhoneNumber, "", new { @class = "text-danger" })
     </div>
</div>

When I try to save the patient record, I am getting the following error:

Violation of PRIMARY KEY constraint 'PK_dbo.PatientInsurrances'. Cannot insert duplicate key in object 'dbo.PatientInsurrances'. The duplicate key value is (0).\r\nThe statement has been terminated.

Here is my controller:

public ActionResult Create(Patient patient)
{
    if (ModelState.IsValid)
    {
        db.Patients.Add(patient);
        db.SaveChanges();

        return RedirectToAction("Index");
    }

    return View(patient);
}

It is attempting to create a patientinsurancerecord, but it can't because we do not yet know the patientId because we are creating the patient record at this time. Furthermore, patientinsurance is not required and it is possible to create a patient without this patientinsurance.

So the question is, how do I add fields to a view that collect data to be stored in multiple tables?

How do I create foreign key relationships that do not require data for the related table?

  • Does your PatientInsurrances table have an ID column? If yes, is it set to auto increment? Otherwise, if you are not explicitly setting this field to a unique ID, it could be defaulting to zero. – Tot Zam Mar 08 '17 at 21:41
  • Yep, here's the Patient modle: public class Patient { public string applicationUserId { get; set; } [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int patientId { get; set; } and then my PatientInsurance mdoel: [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int id { get; set; } [Key] public int patientId { get; set; }. I am honestly not sure how the relationship is working behind the scenes. Both tables have a patientId column. – texasnewbie Mar 08 '17 at 21:41
  • I'm referring to in your database. Does the PatientInsurrances table in your database have an ID column that is not set to auto increment? – Tot Zam Mar 08 '17 at 21:42
  • Possible duplicate of ["Violation of PRIMARY KEY constraint 'PK\_Vehicle\_Transactions'. Cannot insert duplicate key in object 'dbo.Vehicle\_Transactions"](http://stackoverflow.com/questions/24425021/violation-of-primary-key-constraint-pk-vehicle-transactions-cannot-insert-du) – Tot Zam Mar 08 '17 at 21:45
  • It does, id is an Identity field, – texasnewbie Mar 08 '17 at 21:45
  • I thought about that approach, but I do have questions around the patientinsurrance record being created. What if I don't want to save any patientinsurance data? – texasnewbie Mar 08 '17 at 21:47
  • There are many duplicate questions that should help you solve your problem: https://www.google.com/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=violation+of+primary+key+constraint+the+duplicate+key+value+is+(0)&* – Tot Zam Mar 08 '17 at 21:48
  • I'm not exactly sure what you mean by not wanting to save any of the data. If you don't want to save it, leave out `db.Patients.Add(patient);` or `db.SaveChanges()` if a certain condition is met. – Tot Zam Mar 08 '17 at 21:50
  • The patient object contains patientinsurance. When I call db.Patients.Add(patient) it is saving to both patient and patientinsurance. I'm getting the error because the key in patientinsurance patientid is not yet populated because it's creating both records at the same time. I'm curious about how to do this if the end user does not populate any insurance data. It will complain because patientinsurance is empty. Is this because patientId is a primary key in the patientinsurance table? – texasnewbie Mar 08 '17 at 21:55
  • It's creating the patientinsurance record with patientid = 0 because the patient record is being created at the same time. It does not know what the patientid of the patient record is yet. – texasnewbie Mar 08 '17 at 22:20

0 Answers0