如何使用entityframework插入多个表 [英] how do insert into multiple tables with entityframework

查看:174
本文介绍了如何使用entityframework插入多个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用角色会员网络应用程序。我创建了一个自定义角色提供程序和成员资格我的注册页面运作完美。我从注册中收集所有必要的信息。我登录到后台并为用户分配适当的角色。



我的表是用户,角色,用户角色,医生,患者,医学,行为和评论者。



userinrole对用户和角色有一个fk

评论者有fk到用户和用户角色

医生有fk to user,userinroles and reviewers

患者有fk给医生

医疗,行为,评估和药物治疗对病人有好处



此时我们只有一位评论员如此指派所有医生。

我需要知道如何实现这一点并确保当医生填写患者表格时它将登录身份证的医生插入患者fk,为医生和所有患者提供fk for。



我试过这个但是得到了错误。 />


私人实体db =新实体();







db.AddToPatients(患者);

db.AddToMedic al(医疗);

等等。



db.SaveChanges();







收到错误消息

INSERT语句与FOREIGN KEY约束\FK_Patients_Doctors \冲突。冲突发生在数据库\db \,表\dbo.Doctors \,列'DoctorsId'。\\\\ n该声明已被终止。}



我相信我明白它正试图为医生创建一个新的记录,但身份证已经存在。我该如何阻止这个。

I am using role membership web application. I create a custom role provider and membership provider. MY registration page works perfectly. I collect all the necessary information from registration. I log on to the back office and assign user to the proper role.

My tables are user, roles, userinroles, doctors, patients, med, behavior,and reviewers.

userinrole has a fk to user and roles
reviewer has fk to user and userinroles
doctors has fk to user, userinroles and reviewers
patients has fk to doctors
medical, behavior, assessment, and medication has fk to patients

At this moment we only have one reviewer so fk into be assign all doctors.
I need to know how make this happen and make sure that when a doctors fill out the patient form it insert the doctor that is log on id into the patients fk for doctors and all the patients fk for were needed.

I have tried this but getting error.

private entities db = new entities();



db.AddToPatients(patients);
db.AddToMedical(medical);
and so on.

db.SaveChanges();

I

getting error message
The INSERT statement conflicted with the FOREIGN KEY constraint \"FK_Patients_Doctors\". The conflict occurred in database \"db\", table \"dbo.Doctors\", column 'DoctorsId'.\r\nThe statement has been terminated."}

I believe I understand that it is trying to create a new record for the doctors but the id already exist. How do I stop this.

推荐答案

这里的一些例子

实体框架性能 [ ^ ]
Some examples here
Entity Framework Performance[^]


这篇关于如何使用entityframework插入多个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆