Sql server 使用实体框架添加记录
我在VS2010中使用EF4,我有以下代码:Sql server 使用实体框架添加记录,sql-server,entity-framework,entity-framework-4,edmx,Sql Server,Entity Framework,Entity Framework 4,Edmx,我在VS2010中使用EF4,我有以下代码: PatientInformation patientInformation = PatientInformation.CreatePatientInformation(3); patientInformation.DateOfBirth = new DateTime(1983, 6, 13); patientInformation.FamilyId = 1; patientInformation.FirstName = "First"; patie
PatientInformation patientInformation = PatientInformation.CreatePatientInformation(3);
patientInformation.DateOfBirth = new DateTime(1983, 6, 13);
patientInformation.FamilyId = 1;
patientInformation.FirstName = "First";
patientInformation.LastName = "Patient";
ClinicEntity.PatientInformations.AddObject(patientInformation);
ClinicEntity.SaveChanges();
问题是AddObject()
没有将对象添加到集合中,我就是不明白为什么。它也没有给出任何错误
我的SQL Server数据库中唯一不可为空的字段是PatientId
列,它是一个标识列。。。请帮忙
提前谢谢
Yogesh Lotlikar试试:
ClinicEntity.PatientInformations.Add(patientInformation);
我不知道这句话到底是怎么回事:
PatientInformation patientInformation = PatientInformation.CreatePatientInformation(3);
如果您将这样做作为测试,会发生什么情况:
PatientInformation patientInformation = new PatientInformation();
patientInformation.DateOfBirth = new DateTime(1983, 6, 13);
patientInformation.FamilyId = 1;
patientInformation.FirstName = "First";
patientInformation.LastName = "Patient";
ClinicEntity.PatientInformations.AddObject(patientInformation);
ClinicEntity.SaveChanges();
如果测试代码正常工作,您需要重新编写CreatePatientInformation(3)调用中发生的任何事情。我没有得到.Add方法。它只告诉我。AddObject()AddObject不工作。我甚至尝试过使用下面的代码ClinicEntity.addtopatientformations(patientInformation);虽然这是一个不推荐使用的方法,但它既不会给出错误,也不会在数据库中插入记录。@Yogi,虽然您使用的是EF 4.1+,但您应该启动sql profiler的副本,然后查看对数据库的调用(如果有)。复制您捕获的生成的sql并从SSMS运行它,然后查看是否得到任何更好的错误消息;没有为插入此记录生成任何条目。请向我们展示
AddObject()
的实现。我假设它是自定义实现的?你能告诉我们你在EF数据库中使用的连接字符串吗?@marc_s这里是连接字符串-@KomengeMwandila-它不是自定义实现。AddObject附带由实体类继承的ObjectContext类