开发者

How do i write this logic in ria services?

开发者 https://www.devze.com 2023-02-14 20:09 出处:网络
How do i write this logic in ria services? At which place? The pseudocode for sql is :- If exists (select recordid from table where recordid = @recordId)

How do i write this logic in ria services? At which place?

The pseudocode for sql is :-

If exists (select recordid from table where recordid = @recordId)
    update the record
else
   insert the record.

The RIA se开发者_StackOverflow中文版rvices offer just standard CRUD pattern. Do i have to use Invoke for this? If i use Invoke i will not get the benefits that RIA services offer like change tracking etc. Using Invoke means using RIA service like WCF service.

Thanks in advance :)


There is no reason you cannot extend the boiler plate insert methods RIA generates with your own semantics. The code below I put together as a quick eg for a method to do what you were look for.

I would suggest that you make sure that this route doesn't cause you issues. I have observed that clientside RIA tends to have a of objects in its contexts after performing operations.

Hope this helps.

public void InsertFoo ( Foo foo )
{
  using ( FooEntities db = new FooEntities() )
  {
    var fooRecord = db.Foos.Where( a => a.fooId == foo.fooId ).SingleOrDefault();

    if (fooRecord.Any())
    {
      // Insert
      if ( ( foo.EntityState != EntityState.Detached ) )
        this.ObjectContext.ObjectStateManager.ChangeObjectState( foo, EntityState.Added );
      else
        this.ObjectContext.SystemAccounts.AddObject( foo );
    }
    else
    {
      // Update
      fooRecord.Field = foo.Field;
      db.SaveChanges();

      this.ObjectContext.SystemAccounts.AttachAsModified( foo, this.ChangeSet.GetOriginal( foo ) );
    }
  }
}
0

精彩评论

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