locked
using seed method in migration RRS feed

  • Question

  • User-1474096950 posted

    initally using code first approch i created db and tables . inserted few records using the form

    now i am trying to use seed method in configuration.cs to added more test data .

    but having issue with key. below is patient and patient address model having  1 - 1 relationship

    patient model

     public int PatientID { get; set; }
    
             
             [Display(Name = "Last Name")]
            public string LastName { get; set; }
    
            
             [Display(Name = "First Name")]
            public string FirstName { get; set; }
    
            public float age { get; set; }
            public string Mobile { get; set; }
    
      public virtual PatientAddress PatientAddress { get; set; }

    patient address model

     [Key]
            [ForeignKey("Patient")]
            public int PatientID { get; set; }
    
            public string Address1 { get; set; }
            public string Address2 { get; set; }
            public string City { get; set; }
            public string State { get; set; }
            
           
           public virtual Patient Patient { get; set; }

    when  adding records in the address using seed method how do i refer the patientid?

    seed method of config.cs

     context.PatientAddress.AddOrUpdate(
                   p => p.PatientID,
                   new PatientAddress() { Address1 = "Address1  ", Address2 = "", City = "City1", State = "State 2"  },
                   new PatientAddress() { Address1 = "Address1 22 ", Address2 = "Address2 44", City = "City2", State = "State 33"  },
                   new PatientAddress() { Address1 = "Address1 777 ", Address2 = "Address2 66", City = "City3", State = "State 44" },
                   new PatientAddress() {Address1 = "Address1 44 ", Address2 = "Address2 477", City = "City4", State = "State 232"  }
                   );

    Saturday, April 29, 2017 7:33 AM

All replies

  • User-1838255255 posted

    Hi svibuk,

    when  adding records in the address using seed method how do i refer the patientid?

    According to your description and codes, if you have already set the One-to-Zero-or-One Relationship with the patient, you don't need to set the patientid.

    You could just set the Patient type's value.

    More details, you could refer to below codes:

        protected override void Seed(MVCWebSite.Models.ApplicationDbContext context)
            {         
                patient p1 = new patient();
                p1.age = 11;
                p1.FirstName = "aa";
                p1.LastName = "bb";
                p1.Mobile = "cccc";
                p1.PatientID = 1;
                PatientAddress p2 = new PatientAddress();
                p2.Address1 = "aa";
                p2.Address2 = "b";
                p2.City = "c";
                p2.State = "aa";
                //set the forgin key model
                p2.patient = p1;
                //set the forgin key model
                p1.PatientAddress = p2;
    
                context.address1.AddOrUpdate(p2);
            }

    Result:

    Best Regards,

    Eric

    Monday, May 1, 2017 2:49 AM
  • User-1474096950 posted

    ok i will try

    Monday, May 1, 2017 9:55 AM