linq2sql 如何处理只有 FK 的表?

发布于 2024-08-11 12:17:20 字数 109 浏览 1 评论 0原文

我有一个映射表,例如:

Article_to_Categories

ArticleID CategoryID

如何使用 linq2sql 插入到该表中?

I have a mapping table like:

Article_to_Categories

ArticleID
CategoryID

How would I insert into this table using linq2sql?

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

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

发布评论

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

评论(2

待"谢繁草 2024-08-18 12:17:20

如果文章和类别表中存在相应的行,则这应该与标准插入没有什么不同。

另一方面,如果这些表为空,则您需要在执行 SubmitChanges 之前将行插入到这些表中。

Linq-to-Sql 将为您管理优先级。

总而言之,我建议您尝试将行插入到 Article_to_Categories 表中,看看会发生什么。


编辑:如果不了解如何在 Linq-to-Sql 中插入行,请考虑网络上的示例:

Provided that corresponding rows exist in the Article and Category tables, this should be no different than a standard insert.

On the other hand, if those tables are empty, then you'll need to insert rows into those tables before you execute SubmitChanges.

Linq-to-Sql will manage the precedence for you.

All in all, I suggest that you simply try to insert rows into the Article_to_Categories table and see what happens.


EDIT: If don't understand how to insert a row in Linq-to-Sql, consider examples on the web:

梦与时光遇 2024-08-18 12:17:20

根据我的经验,在像这样的关联表中插入记录时,我也遇到过类似的问题。对我有用的是尊重外键关系并添加对象而不仅仅是外键 ID。

例如,这对我不起作用:

var newRec = new Article_To_Categories
{
    Article_ID = 1,
    Category_ID = 2
};

DataContext.Article_To_Categories.InsertOnSubmit(newRec);
DataContext.SubmitChanges();

对我有用的是:

var newRec = new Article_To_Categories
{
    Article = DataContext.Articles.Where(a => a.Article_ID == 1).SingleOrDefault(),
    Category = DataContext.Articles.Where(a => a.Category_ID == 2).SingleOrDefault()
};

DataContext.Article_To_Categories.InsertOnSubmit(newRec);
DataContext.SubmitChanges();

根据我的研究,如果我添加对象而不是 ID,生成的 Article_To_Categories 对象将正确更新为相关记录。更新过程的一部分是通知不应重新插入相关记录。如果您只是使用 ID 更新新记录,我不相信 LINQ-to-SQL 会自动从 Article 和 Category 表中检索记录,这意味着这些相关记录将受到任何现有验证的约束,无论是来自对象或数据库。

In my experience, I have had similar problems inserting records in association tables like this one. What works for me is to honor the foreign key relationship and add objects rather than just the foreign key ID.

For example, this didn't work for me:

var newRec = new Article_To_Categories
{
    Article_ID = 1,
    Category_ID = 2
};

DataContext.Article_To_Categories.InsertOnSubmit(newRec);
DataContext.SubmitChanges();

What worked for me was this:

var newRec = new Article_To_Categories
{
    Article = DataContext.Articles.Where(a => a.Article_ID == 1).SingleOrDefault(),
    Category = DataContext.Articles.Where(a => a.Category_ID == 2).SingleOrDefault()
};

DataContext.Article_To_Categories.InsertOnSubmit(newRec);
DataContext.SubmitChanges();

As near as I could figure based on my research, if I add the object rather than the ID, the resulting Article_To_Categories object is correctly updated with the contents of the related records. Part of the updating process is the notification that the related records should not be re-inserted. If you just update the new record with the ID's, I don't believe that LINQ-to-SQL will automatically retrieve the records from the Article and Category tables, which means that those related records will be subject to any existing validation, whether from the object or the database.

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