开发者

Insert Entity into EntityCollection w/ RIA

开发者 https://www.devze.com 2023-01-12 00:58 出处:网络
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

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 mor开发者_如何学Pythone 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:)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消