我对 linq toEntity 中的 find 方法感到困惑。进入第三级儿童
你好,我会尽量说得非常具体,因为这对我来说非常重要。 假设您有以下表结构:
以下代码使用 find 将数据插入到特定的 MvrMeds方法。 find 方法找到主键 PKMvrMedsId,然后关联表,如下所示:
MvrMed MvrMedsTable = DbCtx.MvrMeds.Find(1);
MvrMedsDispensingError DispensingErrorsTable = new MvrMedsDispensingError { MvrMedsDispensingErrorsNameId=2 };
MvrMedsAdministrationError AdministrationErrorsTable = new
MvrMedsTable.MvrMedsDispensingErrors.Add(DispensingErrorsTable);
DbCtx.SaveChanges();
但我想知道如何使用 Mvrs 表进行与上面相同的插入:
我的第一次尝试
Mvr MvrTable = DbCtx.Mvrs.Find(1).include("MvrMeds").include("MvrMedsAdministrationErrors");//intellisense said no to this.
第二次尝试
Mvr MvrTable = DbCtx.Mvrs.Find(1);
var query =(from x in MvrTable.MvrMeds
select x);
MvrMedsDispensingError DispensingErrorsTable1 = new MvrMedsDispensingError { MvrMedsDispensingErrorsNameId = 2 };
query.MvrMedsDispensingError.add(DispensingErrorsTable1);
DbCtx.SaveChanges();
并且知道我不确定如何处理这个问题而不会使这变得过于复杂。
Hello will try to be as very specific because this is very important to me.
Suppose you have the following table structre:
The following code inserts the data to a specific MvrMeds by using the find method. The find method finds the primary key PKMvrMedsId and then I associate the tables as shown below:
MvrMed MvrMedsTable = DbCtx.MvrMeds.Find(1);
MvrMedsDispensingError DispensingErrorsTable = new MvrMedsDispensingError { MvrMedsDispensingErrorsNameId=2 };
MvrMedsAdministrationError AdministrationErrorsTable = new
MvrMedsTable.MvrMedsDispensingErrors.Add(DispensingErrorsTable);
DbCtx.SaveChanges();
But I am wondering how would I use Mvrs table do the same insert as above:
My First Attempt
Mvr MvrTable = DbCtx.Mvrs.Find(1).include("MvrMeds").include("MvrMedsAdministrationErrors");//intellisense said no to this.
Second Attempt
Mvr MvrTable = DbCtx.Mvrs.Find(1);
var query =(from x in MvrTable.MvrMeds
select x);
MvrMedsDispensingError DispensingErrorsTable1 = new MvrMedsDispensingError { MvrMedsDispensingErrorsNameId = 2 };
query.MvrMedsDispensingError.add(DispensingErrorsTable1);
DbCtx.SaveChanges();
And know I am unsure of how to aproach this without overcomplicating this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的上下文应该有 n 个 Add 方法,其中 n 是模型上的实体数量。首先创建新实体来表示需要放入表中的记录,然后使用相应的 Add 方法将它们添加到上下文中。然后,通过查找现有记录(通过 find 或 .where 查询)将新记录与现有记录相关联,并使用关联导航属性设置器通过记录关系将记录绑定在一起。最后调用保存更改。
这应该很接近...
Your context should have n Add methods where n is the number of entities on your model. First create new entities to represent the records that need to go in your tables and then add them to the context using the respective Add method. Then associate the new records to your existing record by looking up the existing one (via find or .where query) and use the association navigation property setters to tie the records together by their relationships. Finally call save changes.
This should be pretty close...