Linq-to-sql 一次添加项目和一对多记录

发布于 2024-08-29 02:55:21 字数 618 浏览 4 评论 0原文

我有一个可以添加文章并且用户可以对其发表评论的功能。这是通过一对多关系来完成的,例如 = "commentId=>ArticleId"。但是,当我尝试在添加一对多记录的同时将评论添加到数据库时,commentId 未知。就像这段代码:

Comment comment = new Comment();
comment.Date = DateTime.UtcNow;
comment.Text = text;
comment.UserId = userId;
db.Comments.InsertOnSubmit(comment);
comment.Articles.Add(new CommentsForArticle() { ArticleId = articleId, CommentId = comment.CommentId });

在我按下提交之前,commentId 将为 0。有什么方法可以不必在两者之间提交,或者我只需剪掉具有一对多关系的部分,然后仅使用 CommentTable 和诸如 之类的列“文章 ID”

从性能角度来看什么是最好的?

我了解根本问题,我只是想知道哪种解决方案最有效。

I have a function where I can add articles and users can comment on them. This is done with a one to many relationship like= "commentId=>ArticleId". However when I try to add the comment to the database at the same time as I add the one to many record, the commentId is not known. Like this code:

Comment comment = new Comment();
comment.Date = DateTime.UtcNow;
comment.Text = text;
comment.UserId = userId;
db.Comments.InsertOnSubmit(comment);
comment.Articles.Add(new CommentsForArticle() { ArticleId = articleId, CommentId = comment.CommentId });

The commentId will be 0 before i press submit. Is there any way arround not having to submit in between or do I simply have to cut out the part where I have a one-to-many relationship and just use a CommentTable with a column like "ArticleId".

What is best in a performance perspective?

I understand the underlying issue, I just want to know which solution works best.

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

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

发布评论

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

评论(1

注定孤独终老 2024-09-05 02:55:21

你在问两个不同的问题。

首先,如果评论只能针对一篇文章,那么从数据库设计的角度来看,在 Comment 表中包含 ArticleId 比创建链接表更有意义。中间链接表通常仅用于多对多关系,因为它们无法显式建模并且需要这种间接级别。

话虽这么说,如果您正确定义了外键,那么 Linq2SQL 将检测它们并允许您使用对象而不是键(或者您可以使用其中任何一个)。例如,假设您保持数据库设计不变,最后一行可能是这样的:

comment.Articles.Add(new CommentsForArticle() { ArticleId = articleId, Comment = comment }

请注意,我使用的是实际的 Comment 对象而不是它的 ID。通过这种方式,Linq2SQL 可以自动为您完成该映射。如果您有实际文章的句柄,则也可以将 ArticleId =articleID 替换为 Article =article

You're asking two different questions.

First, if a comment can only be for one article, it makes much more sense from a database design perspective to have ArticleId in the Comment table rather than creating a link table. An intermediate link table is typically only used for many:many relationships, since they can't be modeled explicitly and need that level of indirection.

That being said, if you have your foreign keys defined properly, then Linq2SQL will detect them and allow you to work with objects rather than keys (or you can use either). For example, your last line could be something like this, assuming you left your database design as-is:

comment.Articles.Add(new CommentsForArticle() { ArticleId = articleId, Comment = comment }

Notice that I'm using the actual Comment object rather than its ID. Doing it this way, Linq2SQL can automatically do that mapping for you. If you have a handle to the actual article, you could replace ArticleId = articleID with Article = article as well.

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