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 services 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 ) );
}
}
}