使用实体框架将数据插入相关表中

问题描述 投票:0回答:1

我有两个相关的表ProcessBatches和Transfers enter image description here

首先,我必须在ProcessBatches中插入记录,然后插入Transfers。我正在使用以下代码:

transferData.ProcessBatch = Context.ProcessBatches.Add(processBatchData);
Context.Transfers.Add(transferData);
Context.SaveChanges();

我的问题:在最糟糕的情况下,是否会在ProcessBatches中插入记录而在Transfers中不插入记录?

我有另一个想法:首先,在ProcessBatches中插入记录,然后询问id,最后插入Transfers(如果id正确生成)

注意:两个表的id都是自动生成的。

entity-framework entity-framework-6
1个回答
0
投票

我认为你需要这样做:

transferData.ProcessBatch = processBatchData;
Context.Transfers.Add(transferData);
Context.ProcessBatches.Add(processBatchData);
Context.SaveChanges();

我相信Add是一个Void方法,所以它不会返回任何东西。在您的代码中,您基本上将transferData.ProcessBatch设置为null。

© www.soinside.com 2019 - 2024. All rights reserved.