如何使用 Ria 服务通过 EF 添加具有外键的实体?

发布于 2024-08-22 15:34:12 字数 318 浏览 2 评论 0原文

我的数据库中有两个表:书籍和类别。在我的 EDMX 中,我生成了 Book 和 Category 类,以及相应的 BookSet 和 CategorySet。我创建了 BookService,并且可以通过 BookContext 在我的 SilverLight 代码中访问它。

通过 BookContext 我可以加载所有书籍的列表。我现在的问题是我正在尝试通过此上下文将一本书添加到我的数据库中。但是,当我尝试添加类别信息以满足外键约束时,我无法这样做。

我在 Google 上进行了广泛的搜索,但我无法看到如何使用 POCO 执行此操作的代码。有人可以帮忙吗?

提前致谢

I have two tables in my database: Books and Categories. In my EDMX I have generated classes Book and Category, and correspondingly BookSet and CategorySet. I created BookService and can access it in my SilverLight code via BookContext.

Through BookContext I am able to load a list of all books. My problem right now is I am trying to add a Book to my database via this context. However, when I try to add the category information to suffice the foreign key constraint, I am not able to do so.

I have searched Google far and wide but I am not able to see code for how to do this with a POCO. Can anyone assist?

Thanks in advance

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

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

发布评论

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

评论(1

苦行僧 2024-08-29 15:34:12

我很确定它会是这样的。

Book book = new Book();
Category category = new Category();
book.Title = title;

BookContext.Books.Add(book);
book.Category = category;

SubmitChanges(null);

I'm pretty sure that it would be something like this.

Book book = new Book();
Category category = new Category();
book.Title = title;

BookContext.Books.Add(book);
book.Category = category;

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