在 WCF 数据服务中保存外键,但不保存整个对象

发布于 2024-10-06 00:58:31 字数 703 浏览 2 评论 0原文

我正在使用 Silverlight 4、Entity Framework 4 和 WCF 数据服务。 假设我有一个 Playlist 对象。该播放列表对象具有属性和外键,外键由显示对象的 0 到 1 导航属性表示。数据库中有一个 Display_Id 列。

我试图保存播放列表并直接设置 display_id,而不从数据库加载整个显示对象(我从查询字符串中获取显示 id)。 我已经尝试过:

playlist.Display = new Display() { Id = 3136 };
// this SetLink throws an exception that the Display is not yet tracked    
context.SetLink(playlist.Display, "Display", playlist);
// or i've tried, but get an error: Entities in 'EDM.Displays' participate in the //'DisplayX' relationship. 0 related 'X' were found. 1 'X' is expected
context.AddToDisplays(playlist.Display);
context.SetLink(playlist, "Display", playlist.Display);

我需要更改我的 EDM 还是有办法在客户端执行此操作?

I'm using Silverlight 4, Entity Framework 4 and WCF Data Services.
Say I have a Playlist object. That Playlist object has properties and a Foreign Key represented by a 0 to 1 navigation property to a Display Object. In the database there is a Display_Id column.

I'm trying to save the playlist and setting the display_id directly, without loading a whole display object from the database (I get the display id from the query string).
I've tried :

playlist.Display = new Display() { Id = 3136 };
// this SetLink throws an exception that the Display is not yet tracked    
context.SetLink(playlist.Display, "Display", playlist);
// or i've tried, but get an error: Entities in 'EDM.Displays' participate in the //'DisplayX' relationship. 0 related 'X' were found. 1 'X' is expected
context.AddToDisplays(playlist.Display);
context.SetLink(playlist, "Display", playlist.Display);

Do I need to change my EDM or is there a way to do this on the client side?

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

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

发布评论

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

评论(1

我家小可爱 2024-10-13 00:58:31

首先你需要将对象附加到ObjectSet

Display d = new Display{ id = 3136 };
context.Displays.Attach(d);

,它将在本地执行而无需调用数据库,然后你可以根据需要工作

还有另一种方法:对象播放列表包含对象DysplayReference,其属性EntityKey如果为null则创建它,否则只需替换key在 EntityKeyValues 中

first of all You need to attach object to ObjectSet

Display d = new Display{ id = 3136 };
context.Displays.Attach(d);

it would executed locally without calling database, and then you can work as you need

there is another way: object playlist contains object DysplayReference with property EntityKey if it's null then create it, else just replace key in EntityKeyValues

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