c# - 添加新记录时如何启动外键?

标签 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**
                };

谢谢!

最佳答案

您的外键引用是什么?患者的身份证件? 如果 ID 是 Identity,则只需在 db.SaveChanges();

之后获取 patent.Id

关于c# - 添加新记录时如何启动外键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30806989/

相关文章:

java - DBCP XAMPP 不接受我的时区

c# - 使用 Entity Framework 6 和 C# 调用现有存储过程

domain-driven-design - 删除引用的实体时删除实体

c# - 替换 ICollection 中的元素

c# - 如何使用 AutoMapper 为特定映射映射空值?

c# - .NET 4.5 中的垃圾收集是否有变化

mysql - 我可以在 MySQL 中重复使用具有多个条件的 where 子句吗?

c# - 使用 DirectorySearcher 时如何确定 ClientTimeout

mysql - 端口是如何工作的?

entity-framework - 使用新映射规则扩展 EF 6.2