mvc 发布新表并在同一 post 命令中的另一个表中记录该表 ID 时出现问题
好吧,我尝试了多种不同的方法,有时有效,有时无效,所以我很想找出确保此过程正常工作的可靠方法。
我有 4 个表,其中 1 个表存储每个页面提交时的其他 3 个表 Id。
表一与表二同时创建,表二是表单表,表一只是将所有 4 个表连接在一起的一种方式,
One O = new One();
_db.Twos.AddObject(T);
O.TId = T.Id;
_db.Ones.AddObject(O);
_db.SaveChanges();
这可以工作,但其他 2 个表存在问题,其中 3 个表工作,4 个表平衡,代码如下3.1
我讨厌使用的解决方案是通过会话或模型对象传递每个表直到最后,然后将它们全部提交在一起,但这会带来更多问题,我认为。
编辑-
我不太确定我还可以添加什么,但基本问题是我有 1 个核心表和 3 个子表,并且在不同时间在每个表中创建新记录。
3 保存
第 1 个 = 新核心记录 + 1 个新子表记录 - 有效
第 2 个 = 更新核心记录 + 1 个新子表记录 - 有效
第 3 个 = 更新核心记录 + 1 个新子表记录 - 破坏
第 2/3 个保存示例代码,其中 Th 为传入的表对象代表第三个表,O 是核心表,如顶部附近的代码所示。
O.Three = Th;
_db.SaveChanges();
这正确地提取 ID 并将其保存到第二次保存时的核心表,但不是在第二次保存时将其保存到核心表。第三次保存,即使它是相同的代码并且表具有相同的连接。
每次拉取 - OId 都是核心表 Id,因此每个表也知道保存在哪里。
public ActionResult Three(int OId, FormCollection fc)
每次推送
[HttpPost]
public ActionResult Three(Three Th, FormCollection fc)
除了引用更改之外什么都没有,我认为这就是相关的其他内容,因为它基本上将日期/时间分配给表值,然后在尝试保存更改之前检查 ModelState.IsValid 是否有效。
我也尝试过使用 UpdateModel 但这也不起作用,所以有人知道任何其他解决方案吗?
Ok so I've tried a number of different ways which work some times and don't others so I'd rly like to find out the sure fire way to ensure this process works correctly.
I have 4 tables with 1 table storing the other 3 tables Id upon each page submit.
Table One is created at the same time as table Two with table two being the form table and one just being a way to connect all 4 together
One O = new One();
_db.Twos.AddObject(T);
O.TId = T.Id;
_db.Ones.AddObject(O);
_db.SaveChanges();
this works but the other 2 tables have issues with 3 working and 4 breaking even with the same code as 3.
1 solution that I'd hate to use would be to pass each table via session or a model object right till the end and then submit them all together but that would open it up to more problems I think.
Edit-
I'm not exactly sure what else I can add but the basic problem is I have 1 core table with 3 sub tables and new records gets created in each table at different times.
3 saves
1st = new core record + 1 new sub table record - works
2nd = update core record + 1 new sub table record - works
3rd = update core record + 1 new sub table record - breaks
2nd/3rd save example code with Th being the table object passed in which represents the 3rd table and O being the core table as shown in the code near the top
O.Three = Th;
_db.SaveChanges();
This correctly pulls the ID and saves it to the core table on the 2nd save but not on the 3rd save even tho it's the same code and the tables have the same connections.
Each pull - OId being the Core tables Id so each table knows where to save too.
public ActionResult Three(int OId, FormCollection fc)
Each Push
[HttpPost]
public ActionResult Three(Three Th, FormCollection fc)
As nothing else but the references changes I think thats about it else which is relevant since everything it basically date/time assignments to table values which then get checked if ModelState.IsValid before it trys to save changes.
I've also tried to use UpdateModel but that hasn't worked either so anybody know of any other solutions for this ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
O.TId = T.Id 行中 T.id 的数据库 ID此时在您的代码中将不相关。您必须调用 saveChanges();将 id 输入到对象中。
尝试:
The id from the database for T.id in the line O.TId = T.Id; will not be relevant at that point in your code. You must call saveChanges(); for the id to be fed into the object.
Try: