EntityFramework、RIA 插入问题 - 更新子对象导致插入

发布于 2024-08-17 03:26:11 字数 1239 浏览 0 评论 0原文

我有一个数据库,其中有一个名为 m_AttentionFlags 的表,该表引用 m_PermissionCollections 中的主键。我由此构建了一个 EntityFramework 模型并创建了一个 DomainService。

现在,在客户端上使用 RIA,我已在控件初始化时将标志加载到本地上下文中:

var query = _context.GetM_AttentionFlagQuery(); 
_context.Load(query, OnGetM_AttentionFlag, null); 

然后我连接了一个按钮,将 m_PermissionCollection 添加到选定的 m_AttentionFlag。

if (lstSelected.SelectedItem != null) { 
if (!(lstSelected.SelectedItem is m_AttentionFlag)) 
{ 
return; 
} 
m_AttentionFlag flag = lstSelected.SelectedItem as m_AttentionFlag; 
m_PermissionCollection coll = new m_PermissionCollection(); //** 
flag.m_PermissionCollections = coll; //** 
_context.SubmitChanges(); 
}

奇怪的是,这会导致插入行为:具有相同字段值的附加 m_AttentionFlag 与关联的 m_PermissionCollection 一起添加到数据库中;原始文件保持不变,其 m_PermissionCollections 字段不受影响。 有趣的是,如果您删除标记为 ** 的行并替换为对 m_AttentionFlag 上的简单公共属性的更新,例如 flag.Description = "new description",则更新将按预期进行。

DomainService 方法是向导生成的默认方法,唯一令人感兴趣的是,更改 m_AttentionFlag 上的 m_PermissionCollections 字段会在将项目发送到服务器时将项目上的 EntityState 更改为“已添加”。

我在 RIA 服务论坛上提出了这个问题,一位成员建议我寻找多个 DomainContext 实例,但 _context 成员仅创建一次,并且此处列出的代码几乎是唯一不是由各种 Visual Studio 工具生成的代码。关于这里发生的事情有什么想法吗?

非常感谢帮助。

史蒂夫

I've got a database which has a table called m_AttentionFlags which references a primary key in m_PermissionCollections. I've built an EntityFramework model from this and created a DomainService.

Now using RIA on the client I have loaded the flags into the local context on initialization of a control:

var query = _context.GetM_AttentionFlagQuery(); 
_context.Load(query, OnGetM_AttentionFlag, null); 

Then I've wired up a button to add an m_PermissionCollection to a selected m_AttentionFlag.

if (lstSelected.SelectedItem != null) { 
if (!(lstSelected.SelectedItem is m_AttentionFlag)) 
{ 
return; 
} 
m_AttentionFlag flag = lstSelected.SelectedItem as m_AttentionFlag; 
m_PermissionCollection coll = new m_PermissionCollection(); //** 
flag.m_PermissionCollections = coll; //** 
_context.SubmitChanges(); 
}

Strangely this results in an Insert behaviour: an additional m_AttentionFlag with the same field values is added to the database with an associated m_PermissionCollection; the original remains intact without its m_PermissionCollections field being affected.
Interestingly if you remove the lines marked ** and replace with an update to a simple public property on m_AttentionFlag e.g. flag.Description = "new description", the update proceeds as expected.

The DomainService methods are the default ones generated by the wizards, and the only point of interest is that changing the m_PermissionCollections field on the m_AttentionFlag changes the EntityState on the item to Added when it is sent to the server.

I've asked this question on the RIA services forum and a member suggested I look for multiple DomainContext instances, but the _context member is only created once, and the code listed here is pretty much the only code not generated by the various visual studio tools. Any ideas as to what's happening here?

Help much appreciated.

Steve

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

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

发布评论

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

评论(1

蓝眸 2024-08-24 03:26:11

事实证明,问题出在 VS2010 提供的默认 DomainService 实现和 RIA 服务预览中。

该问题的详细信息如下:

http:// www.riaservicesblog.net/Blog/post/Bug-in-the-Create-DomainService-wizard.aspx

It turns out the problem lay in the default DomainService implementation provided by VS2010 and the RIA services preview.

The problem is detailed here:

http://www.riaservicesblog.net/Blog/post/Bug-in-the-Create-DomainService-wizard.aspx

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