LINQ2SQL 更新数据库,但仅当我在 SQL Server 中查看时

发布于 2024-12-03 04:42:46 字数 1698 浏览 0 评论 0原文

我有一个非常奇怪的问题。

我有3张桌子。

  1. 职位
  2. PositionAttachments
  3. 附件

我不会用键和外键来烦您,因为这应该是很明显的。

如果我添加一个职位、附件和职位附件,一切都会好起来的。

如果我向现有职位记录添加新附件,我会得到以下信息。

  1. 位置表已更新,我可以在屏幕上看到更改
  2. 附件表有新记录
  3. PositionAttachments 表有新记录
  4. 新附件未出现在我的应用程序中的屏幕上
  5. 相同的内容
  6. 关闭应用程序,重新编译并重新运行,我有与 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.

  1. Positions
  2. PositionAttachments
  3. 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.

  1. Positions table is updated and I can see changes on screen
  2. Attachment table has new record
  3. PositionAttachments table has new record
  4. New Attachment does not appear on screen within my application
  5. Close application, recompile and re run and I have the same as 4
  6. 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 技术交流群。

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

发布评论

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

评论(1

海螺姑娘 2024-12-10 04:42:46

只是预感,如果你尝试一下会怎样:

    PositionAttachment positionAttachment = new PositionAttachment(); 
    positionAttachment.Position = positionModel; 
    positionAttachment.Attachmen = attachment; 

虽然我认为 DC 也应该正确获取 ID

Just a hunch, what if you try:

    PositionAttachment positionAttachment = new PositionAttachment(); 
    positionAttachment.Position = positionModel; 
    positionAttachment.Attachmen = attachment; 

Although I think the DC should also pick up the ID's properly

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