如何在添加新记录时启动外键?

时间:2015-06-12 15:36:40

标签: c# mysql entity-framework-6

我有点问题。我想测试两个表的连接。

public class Patient:Person
{
    //inheritance ID
    public string insuranceNumber { get; set; }

    public virtual PatientCard patientCard {get; set;}

}

public class PatientCard
    {

        public PatientCard()
        {
            this.patient = new HashSet<Patient>();
            this.visit = new HashSet<Visit>();
        }

        public long id { get; set; }
        public string bloodType { get; set; }
        public string creationDate { get; set; }

        public virtual ICollection<Patient> patient { get; set; }
        public virtual ICollection<Visit> visit { get; set; }
    }

添加新记录时如何启动外键? 我的尝试看起来像这样......

                var patient = new Patient { 
                    ID=1, name = "Alicja", lastname = "Nowacka", 
                    pesel = "89192232", address = "Szczecin", 
                    phoneNumber = "11233332", insuranceNumber = "3211233123" 
                };
                db.Persons.Add(patient);
                db.SaveChanges();


                var patientCard = new PatientCard { 
                    bloodType = "A", creationDate = "2014-06-16", 
                    patient= //**a reference to the key**
                };

谢谢!

1 个答案:

答案 0 :(得分:0)

您的外键引用是什么?病人身份证? 如果ID是Identity,您只需在patient.Id

之后获得db.SaveChanges();