使用 RIA 将实体插入 EntityCollection

发布于 2024-09-14 03:57:33 字数 1913 浏览 1 评论 0原文

我已经建立了一对多关系。 (例如,拥有许多电话号码的人)。在我的获取查询中,我有 this.ObjectContext.Person.Include("PhoneNumbers") 和生成的元数据,包括 public EntityCollection;电话号码{ 获取;放;我还设置了一个具有此属性和我需要的其他属性的 DTO。

[Include]
[Association("Name","thisKey","otherKey")]
public IEnumerable<PhoneNumbers> PNums { get; set; }

我可以正常检索所有数据,并将其显示在 silverlight 中,但是当我创建新数据时,我遇到了问题。我遇到了这种情况:

private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{

   if (dgMMs.SelectedItem != null)
   {
      PhoneNumbers wb = new PhoneNumbers ();
      wb.this = tbThis.Text;
      wb.that =  tbThat.Text;
      wb.other = tbOther.Text;
      wb.whatnot = tbwhatnot.Text;
      ((Person)dgMMs.SelectedItem).PNums.Add(wb);
   }
}

然后在调用 TDataSource.SubmitChanges(); 时收到此错误:

消息=“提交操作失败 验证。请检查 每个 Entity.ValidationErrors EntitiesInError 中的实体了解更多信息 信息。”

我这样做了,果然有一个错误,但我不太明白为什么会出现这种情况。我在数据库中有一个不可为空的字段,用于last_modified_by字段,但我在设置时没有设置我创建了它并将其添加到实体集合中,我猜这会导致它,但我的问题来自为什么 RIA 不在我创建的服务中调用我的 Insert 方法,因为我想在那里设置该字段。就像这样:

public void InsertPhoneNumber(PhoneNumbers pnum)
{
   pnum.last_modified = DateTime.Today;
   pnum.last_modified_by = Thread.CurrentPrincipal.Identity.Name;
   if ((pnum.EntityState != EntityState.Detached))
   {
      this.ObjectContext.ObjectStateManager.ChangeObjectState(pnum, EntityState.Added);
   }
   else
   {
      this.ObjectContext.PhoneNumbers.AddObject(pnum);
   }
}

但是就像 RIA 添加我的对象并调用它自己的插入方法一样,所以我首先使用它,然后在 UI 中设置属性,然后它会给我这个错误:

消息 =“提交操作失败。 更新时发生错误 条目。请参阅内部异常 细节。内部异常消息: 无法插入显式值 表中的标识列 'iset_trkr_writeback' 时 IDENTITY_INSERT 设置为 OFF。”

我从来没有将身份字段设置为任何内容,我以为 RIA 会为我做这件事。但是当我调试并查看时,它的值是 0。但至少这次它调用了我的在我的服务中插入方法...也许我的流程缺少一些重要的东西,但我确实需要一些帮助,谢谢:)

I've got a one-to-many relationship set up. (Ex. A Person with many Phone Numbers). In my get query i have this.ObjectContext.Person.Include("PhoneNumbers") and the in generated MetaData including public EntityCollection<PhoneNumbers> PhoneNumbers{ get; set; } I have also set up a DTO with this and other properties i need.

[Include]
[Association("Name","thisKey","otherKey")]
public IEnumerable<PhoneNumbers> PNums { get; set; }

I can retrieve all the data alright, and display it in silverlight, but when I create a new one I run into problems. I've got this kind of thing going on:

private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{

   if (dgMMs.SelectedItem != null)
   {
      PhoneNumbers wb = new PhoneNumbers ();
      wb.this = tbThis.Text;
      wb.that =  tbThat.Text;
      wb.other = tbOther.Text;
      wb.whatnot = tbwhatnot.Text;
      ((Person)dgMMs.SelectedItem).PNums.Add(wb);
   }
}

Then I get this error when calling TDataSource.SubmitChanges();:

Message = "Submit operation failed
validation. Please inspect
Entity.ValidationErrors for each
entity in EntitiesInError for more
information."

Alright, So i did that, and sure enough there is an error, but I don't quite understand why there is. I have a non-nullable field in the database for a last_modified_by field which i didn't set when I created it and added it to the entityCollection, and I guess this would be causing it, but my question comes from why RIA doesn't call my Insert method in my service that I've created because I want to set that field there. Like so:

public void InsertPhoneNumber(PhoneNumbers pnum)
{
   pnum.last_modified = DateTime.Today;
   pnum.last_modified_by = Thread.CurrentPrincipal.Identity.Name;
   if ((pnum.EntityState != EntityState.Detached))
   {
      this.ObjectContext.ObjectStateManager.ChangeObjectState(pnum, EntityState.Added);
   }
   else
   {
      this.ObjectContext.PhoneNumbers.AddObject(pnum);
   }
}

But it's like RIA adds my object and calls it own Insert Method. So I rolled with it at first, and just set the property up in the UI, then it would give me this error:

Message = "Submit operation failed. An
error occurred while updating the
entries. See the inner exception for
details. Inner exception message:
Cannot insert explicit value for
identity column in table
'iset_trkr_writeback' when
IDENTITY_INSERT is set to OFF."

I never set the identity field to anything, I thought RIA would do this for me. But when i debug and take a look, it has a 0 for the value. But at least this time it calls my insert method in my service... Maybe I'm missing a big something for my process, but I really could use some help. Thanks:)

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

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

发布评论

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

评论(1

与君绝 2024-09-21 03:57:33

您使用实体框架吗?如果是这样,您的元数据中至少一个字段需要一个 [Key] 属性。或者创建一个身份/PK 列 (int/guid),然后更新元数据。

You using Entity Framework? If so, you need a [Key] attribute on at least one field in your metadata. Or create an identity/PK column (int/guid), and then update the metadata.

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