向关系表添加行 - linq to sql

发布于 2024-11-14 04:45:55 字数 940 浏览 1 评论 0原文

我有 3 个主要表格 - 图片、相册和拼贴CollageAlbum 可以有 1 多张图片。图片不必位于相册 中才能添加到拼贴

为了定义它们的关系,我有 AlbumPictureCollagePicture 表。

我的问题是,当我尝试将已上传到 Collage 的图片添加到 CollagePicture 表时。由于图片已存在,它会在 PK_Picture 上引发主键违规错误。

CollagePictures.InsertOnSubmit(new CollagePicture {Collage = CollagePicture = existingPic});

我需要在 CollagePicture 中为现有图片和新 Collage 添加一条记录。如果图片已经存在,有什么方法可以告诉 linq 不要添加图片吗?

我是 Linq 新手,仍在学习中。

编辑: 抱歉,如果我不清楚。 上传图片时,我将 PK 作为 GUID 分配给图片。在 CollagePicture 中,我已经有了这张带有不同拼贴的 pictureId,

例如

CollagePictureId [PK]= 1
CollageId=1
PictureId = 1234567890123456

现在我想添加具有不同 CollageId 的相同图片,比如 2。当我尝试向 CollagePicture 添加任何内容时,它也会尝试添加到 Picture 表中。那就是我遇到例外的时候。希望这能解决问题。

I have 3 primary Tables - Picture, Album and Collage. Collage and Album can have 1-many pictures. Pictures do not have to be in Album to be added to the Collage.

To define their relationship, I have AlbumPicture and CollagePicture tables.

My problem is when I try to add picture already uploaded to the Collage and hence to the CollagePicture table. It throws primry key violation error on PK_Picture since picture already exists.

CollagePictures.InsertOnSubmit(new CollagePicture {Collage = CollagePicture = existingPic});

I need to add one record in CollagePicture for existing picture and new Collage. Is there any way I can tell linq to not add picture if it already exists?

I am new to Linq and still learning.

EDIT:
sorry If I was not clear.
I assign PK to Picture as GUID when Pictures are uploaded. In CollagePicture I already have this pictureId with different collage

e.g.

CollagePictureId [PK]= 1
CollageId=1
PictureId = 1234567890123456

Now I want to add same Picture with different CollageId say,2. When I try to add anything to CollagePicture it tries to add to the Picture table as well. and that is when I get an exception. Hope this will clear things up.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

最舍不得你 2024-11-21 04:45:55

我终于成功了。

而不是这个,

CollagePictures.InsertOnSubmit(new CollagePicture {Collage = Collage, CollagePicture = existingPic});

它应该是

CollagePictures.InsertOnSubmit(new CollagePicture {Collage = collage, CollagePicture.PictureId = existingPic.PictureId});

当您将对象添加到关系表实体时,它也会在主表中添加该对象。因此,仅引用 Id 即可确保它不会对主表执行任何操作。

这很容易,我应该之前尝试过......但还是感谢大家的帮助。

I finally made it work..

Instead of this

CollagePictures.InsertOnSubmit(new CollagePicture {Collage = Collage, CollagePicture = existingPic});

It should be

CollagePictures.InsertOnSubmit(new CollagePicture {Collage = collage, CollagePicture.PictureId = existingPic.PictureId});

When you add an object to the relational table entity it adds the object in primary table as well. so just referencing Id makes sure that it does not do anything to the primary table.

It was easy and I should have tried that before..but Thanks for everyone's help though.

鸠书 2024-11-21 04:45:55

CollagePicture 表上有什么主键?目前还不清楚,向 CollagePicture 插入新条目时您想要做什么。您的意思是将现有图片添加到现有拼贴画中吗?如果是这样,您的主键应该是 (PictureId, CollageId)

What primary key do you have on CollagePicture table? It's not really clear, what do you want to do when inserting new entry to CollagePicture. Do you mean that you add the existing picture to an existing collage? If so your primary key should be (PictureId, CollageId)

安静被遗忘 2024-11-21 04:45:55

如果没有数据模型的详细视图,就很难做出猜测。

如果正确配置了关系和 LINQ to SQL,则可以执行如下操作:

collage.Add(Picture);

这样 LINQ to SQL 就会将正确的行添加到表中。

要检查图片是否已存在于拼贴中,您可以执行以下操作。

collage.Pictures.Where(p => p.ID == pictureID).Count() > 0

Without a detailed view of your data model, it's really hard to take a guess.

If you correctly configured your relations and LINQ to SQL, you could do something like the following:

collage.Add(Picture);

That way LINQ to SQL would add the right rows to the tables.

To check if the picure already exists in the Collage, you would do something like this.

collage.Pictures.Where(p => p.ID == pictureID).Count() > 0
丶情人眼里出诗心の 2024-11-21 04:45:55

如果 existingPic 的 PK 为 0(假设类型为 int),则它是新的,否则它已经存在。

if(existingPic.pkColumnId == 0)
  CollagePictures.InsertOnSubmit(existingPic);

If the PK of existingPic is 0 (assuming type int) then it is new, else it already exists.

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