LINQ2SQL 更新数据库,但仅当我在 SQL Server 中查看时
我有一个非常奇怪的问题。
我有3张桌子。
- 职位
- PositionAttachments
- 附件
我不会用键和外键来烦您,因为这应该是很明显的。
如果我添加一个新职位、附件和职位附件,一切都会好起来的。
如果我向现有职位记录添加新附件,我会得到以下信息。
- 位置表已更新,我可以在屏幕上看到更改
- 附件表有新记录
- PositionAttachments 表有新记录
- 新附件未出现在我的应用程序中的屏幕上
- 相同的内容
- 关闭应用程序,重新编译并重新运行,我有与 4通过 SQL 查看任何表 服务器和我被踢出会话,一旦我重新登录,我就会在屏幕上看到记录。
任何时候都不会产生错误。
更新记录代码;
public void AddAttachmentToPosition(PositionsAvailable positionModel, Attachment attachment)
{
//attachment.id = Guid.NewGuid();
dc.Attachments.InsertOnSubmit(attachment);
PositionAttachment positionAttachment = new PositionAttachment();
positionAttachment.PositionId = positionModel.PositionId;
positionAttachment.AttachmentId = attachment.id;
//positionAttachment.id = Guid.NewGuid();
dc.PositionAttachments.InsertOnSubmit(positionAttachment);
dc.SubmitChanges();
}
编辑
我查看了数据上下文生成的日志,并得到了 PositionAttachments 插入的日志。附件表看起来基本相同。
DECLARE @output TABLE([id] UniqueIdentifier)
INSERT INTO [dbo].[PositionAttachments]([PositionId], [AttachmentId])
OUTPUT INSERTED.[id] INTO @output
VALUES (@p0, @p1)
SELECT [id] FROM @output
-- @p0: Input UniqueIdentifier (Size = 0; Prec = 0; Scale = 0) [92a3627d-ad01-466e-a315-423c851efc5d]
-- @p1: Input UniqueIdentifier (Size = 0; Prec = 0; Scale = 0) [db566728-a313-40c7-af82-0a2f147234eb]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.5420
在我看来,这看起来很正常。此外,相同的代码在我添加新职位时有效,而不是在我向现有职位添加附件时有效。
I have a really odd problem.
I have 3 tables.
- Positions
- PositionAttachments
- Attachments
I won't bore you with the keys and foreign keys as that should be kinda evident.
If I add a new position, attachment and positionattachment all's well.
If I add a new attachment to an existing Position record i get the following.
- Positions table is updated and I can see changes on screen
- Attachment table has new record
- PositionAttachments table has new record
- New Attachment does not appear on screen within my application
- Close application, recompile and re run and I have the same as 4
- View ANY table through SQL server and I get kicked out of my session and once I log back in I see the records on screen.
At no point is there an error being generated.
Update record code;
public void AddAttachmentToPosition(PositionsAvailable positionModel, Attachment attachment)
{
//attachment.id = Guid.NewGuid();
dc.Attachments.InsertOnSubmit(attachment);
PositionAttachment positionAttachment = new PositionAttachment();
positionAttachment.PositionId = positionModel.PositionId;
positionAttachment.AttachmentId = attachment.id;
//positionAttachment.id = Guid.NewGuid();
dc.PositionAttachments.InsertOnSubmit(positionAttachment);
dc.SubmitChanges();
}
Edit
I have looked at the log produced by the Data context and I get this for the PositionAttachments insert. It looks essentially the same for the Attachments table.
DECLARE @output TABLE([id] UniqueIdentifier)
INSERT INTO [dbo].[PositionAttachments]([PositionId], [AttachmentId])
OUTPUT INSERTED.[id] INTO @output
VALUES (@p0, @p1)
SELECT [id] FROM @output
-- @p0: Input UniqueIdentifier (Size = 0; Prec = 0; Scale = 0) [92a3627d-ad01-466e-a315-423c851efc5d]
-- @p1: Input UniqueIdentifier (Size = 0; Prec = 0; Scale = 0) [db566728-a313-40c7-af82-0a2f147234eb]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.5420
To my mind, this looks pretty normal. And besides, the same code works when I am adding a new Position just not when I am adding attachments to an existing position.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是预感,如果你尝试一下会怎样:
虽然我认为 DC 也应该正确获取 ID
Just a hunch, what if you try:
Although I think the DC should also pick up the ID's properly