我对 linq toEntity 中的 find 方法感到困惑。进入第三级儿童

发布于 2024-12-20 13:54:41 字数 1184 浏览 3 评论 0原文

你好,我会尽量说得非常具体,因为这对我来说非常重要。 假设您有以下表结构:

在此处输入图像描述

以下代码使用 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:

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱已欠费 2024-12-27 13:54:41

您的上下文应该有 n 个 Add 方法,其中 n 是模型上的实体数量。首先创建新实体来表示需要放入表中的记录,然后使用相应的 Add 方法将它们添加到上下文中。然后,通过查找现有记录(通过 find 或 .where 查询)将新记录与现有记录相关联,并使用关联导航属性设置器通过记录关系将记录绑定在一起。最后调用保存更改。

这应该很接近...

DbCtx context = new DbCtx();
int mvrId = 100; // pull some mvr based upon id
Mvrs m = context.Mvrs.Where(mv => mv.PKMvrId == mvrId).FirstOrDefault();

// create new entities and add them to the context
MvrMeds meds = MvrMeds.CreateMvrMeds(0);
context.AddToMvrMeds(meds);

MvrMedsDispensingErrors dError = MvrMedsDispensingErrors .CreateMvrMedsDispensingErrors (0);
context.AddToMvrMedsDispensingErrors(dError);

MvrMedsAdministrationErrors aError = MvrMedsAdministrationErrors.CreateMvrMedsAdministrationErrors(0);
context.AddToMvrMedsAdministrationErrors(aError);

// hook everything together
m.MvrMEds = meds;
meds.Mvrs = m;

meds.MvrMedsDispensingErrors.Add(dError);
dError.MvrMeds = meds;

meds.MvrMedsAdministrationErrors.Add(aError);
aError.MvrMeds = meds;


context.SaveChanges();

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...

DbCtx context = new DbCtx();
int mvrId = 100; // pull some mvr based upon id
Mvrs m = context.Mvrs.Where(mv => mv.PKMvrId == mvrId).FirstOrDefault();

// create new entities and add them to the context
MvrMeds meds = MvrMeds.CreateMvrMeds(0);
context.AddToMvrMeds(meds);

MvrMedsDispensingErrors dError = MvrMedsDispensingErrors .CreateMvrMedsDispensingErrors (0);
context.AddToMvrMedsDispensingErrors(dError);

MvrMedsAdministrationErrors aError = MvrMedsAdministrationErrors.CreateMvrMedsAdministrationErrors(0);
context.AddToMvrMedsAdministrationErrors(aError);

// hook everything together
m.MvrMEds = meds;
meds.Mvrs = m;

meds.MvrMedsDispensingErrors.Add(dError);
dError.MvrMeds = meds;

meds.MvrMedsAdministrationErrors.Add(aError);
aError.MvrMeds = meds;


context.SaveChanges();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文