Print Page | Close Window

Entity And Remote Service Method Call

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=2253
Printed Date: 21-Jun-2026 at 8:29am


Topic: Entity And Remote Service Method Call
Posted By: antonio_sergio
Subject: Entity And Remote Service Method Call
Date Posted: 22-Oct-2010 at 1:56am
Hello everyone,
I'm starting to give the first steps with DevForce and found a difficulty.
When I try to chage my Entity in the server give me an error in the property I'm trying to change.
 
What am I dong wrong???????????
 
Client:
        private void btAlterar_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            string typeName = "MasterDetailsWeb.SalesServiceProvider, MasterDetailsWeb";
            string methodName = "AlterarDados";
            Guid myInvokeGuid = System.Guid.NewGuid();
            mgr.InvokeServerMethodAsync(typeName, methodName, GotData, myInvokeGuid, MySalesOrderHeader);
        }

        void GotData(InvokeServerMethodOperation e)
        {
            MySalesOrderHeader = e.Result as SalesOrderHeader;
        }
 
Property:
 
    #region CustomerID property

    /// <summary>Gets or sets the CustomerID. </summary>
    [Bindable(trueBindingDirection.TwoWay)]
    [Editable(true)]
    [Display(Name="CustomerID", AutoGenerateField=true)]
    [IbVal.RequiredValueVerifier( ErrorMessageResourceName="SalesOrderHeader_CustomerID")]
    [DataMember]
    public int CustomerID {
      get { return PropertyMetadata.CustomerID.GetValue(this); }
      set { PropertyMetadata.CustomerID.SetValue(thisvalue); }
    }
    #endregion CustomerID property
 
Server:
 
        [AllowRpc]
        public static object AlterarDados(IPrincipal principal, EntityManager mgr, params Object[] args)
        {
            if (args == null & args.Length == 0)
                return null;
            SalesOrderHeader order = args[0] as SalesOrderHeader;
            order.CustomerID = 117;
            return order;
        }
 
 
Error:
 
 
 
Sorry for my English :))))
 
Regards,
Sérgio Magalhães



Replies:
Posted By: sbelini
Date Posted: 22-Oct-2010 at 4:01pm
Hi Sergio,
 
You are passing a SalesOrderHeader with entity state Unchanged. However, in the server, this SalesOrderHeader is not attached to any entity manager.
 
You need to attach to an entity manager in the server, or remove it from the entity manager in the client before (i.e. setting its EntityState to Detached).
If you want to save the changes to the datasource after modifying the entity, you should take the first approach:
 
  [AllowRpc]
  public static object AlterarDados(IPrincipal principal, EntityManager mgr, params Object[] args) {
    if (args == null & args.Length == 0) {
      return null;
    }
    NorthwindIBModelManager em = (NorthwindIBModelManager)mgr;
    SalesOrderHeader order = args[0] as SalesOrderHeader;
    mgr.AttachEntity(order);
    order.CustomerID = 117;
    mgr.SaveChanges();
    return order;
  }
 
 
otherwhise, you need to remove it from the entity manager in the client (i.e. setting its EntityState to Detached) before calling InvokeServeMethod. You could also create a clone of the entity (which will have EntityState Detached):
 
 public void btAlterar_Click() {
  string typeName = "DomainModel.RemoteServiceMethods, DomainModel";
  string methodName = "AlterarDados";
  Guid myInvokeGuid = System.Guid.NewGuid();
  mgr.RemoveEntity(MySalesOrderHeader);
  DoAsyncAction(() => { mgr.InvokeServerMethodAsync(typeName, methodName, GotData, myInvokeGuid, MySalerOrderHeader); });
 }
 
#######################################################
 
public void btAlterar_Click() {
  string typeName = "DomainModel.RemoteServiceMethods, DomainModel";
  string methodName = "AlterarDados";
  Guid myInvokeGuid = System.Guid.NewGuid();
  SalesOrderHeader newSalerOrderHeader = (SalesOrderHeader)((ICloneable)MySalerOrderHeader).Clone();
  DoAsyncAction(() => { mgr.InvokeServerMethodAsync(typeName, methodName, GotData, myInvokeGuid, newSalerOrderHeader);});
}
 
 
What exactly are you trying to accomplish? So I can better advise you...
 
Regards,
   Silvio.


Posted By: antonio_sergio
Date Posted: 25-Oct-2010 at 1:46am
Hello Silvio, first of all thanks you for your rapid response.
My idea was to send an object (SalesOrderHeader) to the "server".
In the "server" modify this object and sends it to the client with the modified state.
I managed to do it, but do not know if it's the best way.
 
Client:
void GotData(InvokeServerMethodOperation e)
        {
            EntityCacheState ecs = (EntityCacheState)e.Result;
            ecs.Merge(mgr, new RestoreStrategy(falsefalseMergeStrategy.OverwriteChanges));
        }
 
Server:
 [AllowRpc]
        public static object AlterarDados(IPrincipal principal, EntityManager mgr, params Object[] args)
        {
            if (args == null & args.Length == 0)
                return null;

            AdventureWorksEntities em = (AdventureWorksEntities)mgr;

            SalesOrderHeader order = args[0] as SalesOrderHeader;

            em.AttachEntity(order);
            order.CustomerID = 118;

            EntityCacheState ecs = em.CacheStateManager.GetCacheState();
            return ecs;
        }
 
Regards,
Sérgio Magalhães
 


Posted By: sbelini
Date Posted: 29-Oct-2010 at 12:12pm
Hi Sergio,
 
I'm assuming you don't want to save the changes to the datasource. (no em.Save() in the server side code)
 
Also, is there any particular reason to not edit the entity in the client itself?


Posted By: antonio_sergio
Date Posted: 02-Nov-2010 at 3:45am

Hello Sbelini,
I do not want to write the data in the database (at least for now).
I want to change the data on the server side because I need to read some data from database (not visible on the exemple).

Regards,
Sérgio Magalhães



Posted By: sbelini
Date Posted: 02-Nov-2010 at 9:32am
Sergio,
 
It's still unclear to me...
Despite of that, the solution you proposed looks ok.
 
Silvio.



Print Page | Close Window