New Posts New Posts RSS Feed: Entity And Remote Service Method Call
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Entity And Remote Service Method Call

 Post Reply Post Reply
Author
antonio_sergio View Drop Down
Newbie
Newbie


Joined: 21-Oct-2010
Location: Braga
Posts: 9
Post Options Post Options   Quote antonio_sergio Quote  Post ReplyReply Direct Link To This Post Topic: Entity And Remote Service Method Call
    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
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
antonio_sergio View Drop Down
Newbie
Newbie


Joined: 21-Oct-2010
Location: Braga
Posts: 9
Post Options Post Options   Quote antonio_sergio Quote  Post ReplyReply Direct Link To This Post 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
 


Edited by antonio_sergio - 25-Oct-2010 at 1:49am
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post 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?
Back to Top
antonio_sergio View Drop Down
Newbie
Newbie


Joined: 21-Oct-2010
Location: Braga
Posts: 9
Post Options Post Options   Quote antonio_sergio Quote  Post ReplyReply Direct Link To This Post 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

Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 02-Nov-2010 at 9:32am
Sergio,
 
It's still unclear to me...
Despite of that, the solution you proposed looks ok.
 
Silvio.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down