WCF RIA SubmitChanges doesn't send master prop

2019-06-09 14:56发布

问题:

Look, I have these pretty simple model Master-Detail:

Hand is master of Fingers (finger is a detail of hand)

So on the client side:

Hand hand = domainService.Hands[0];                 //  get some hand, doesn't matter
...
Finger f = new Finger() { f.Id = Guid.NewId() };
f.Hand = hand;                                      //  make connection !!
domainService.Fingers.Add(f);
domainService.SubmitChanges(OnSubmitCompleted, null);   //  error is here

On the Server Side:

public void Insert<T>(T obj)
{
    try
    {
        using (ISession session = _factory.OpenSession())
        using (ITransaction transaction = session.BeginTransaction())
        {
            session.SaveOrUpdate(obj);   //  NHibernate error: not-null property references a null or transient value
            transaction.Commit();
        }
    }
    catch (Exception ex)
    {
        throw ;
    }
}

The problem actually is about not sending associations back via WCF wire. So On the server side the Hand property is NULL, but it shouldn't (violation) - I just want to refresh my finger.Hand property

It's not even a composition - I don't need this cause of its limitations

here is the metaAttribute class:

    [MetadataType(typeof(Finger.FingerMetadata))]
public partial class Finger
{
    //[Required(AllowEmptyStrings = true)]
    //[Exclude]
    public virtual Guid HandID { get; set; }
    //{
    //    get { return Hand.Id; }
    //}

    internal sealed class FingerMetadata
    {
        [Key]
        public Guid Id  { get; set; }

        [Include]
        //[RoundtripOriginal]
        //[ExternalReference]
        [Association("Finger-Hand", "HandID", "Id")]
        //[ConcurrencyCheck]
        //[Composition]
        public Hand Hand { get; set; }
    }
}

[MetadataType(typeof(Hand.HandMetadata))]
public partial class Hand
{
    internal sealed class HandMetadata
    {
        [Key]
        public Guid Id  { get; set; }

        [Include]
        //[ExternalReference]
        [Association("Hand-Finger", "Id", "HandID")]
        //[Composition]
        public IList<Finger> Fingers { get; set; }
    }
}

I saw the same problem here http://forums.silverlight.net/forums/p/205220/480824.aspx, but nobody knows.. Please help!

Thanks!

回答1:

If you are still looking for help with this using RIA services (as opposed to Data services)

I've worked quite a bit with these 2 technologies in the recent past and I provided some information on the SL forums link you posted above:

"One to many association is null on Server side" (forums.silverlight.net)



回答2:

You can do two things:

  1. Apply [composition] to your association

  2. Make update methods for all lower enties Overload protected override bool PersistChangeSet() on the domain service:

    protected override bool PersistChangeSet() 
    { 
        Customer changedCustomer; 
        bool success = true;
        foreach (var item in ChangeSet.ChangeSetEntries) 
        { 
           if (item.Entity.GetType() == typeof(Customer)) 
           { 
              changedCustomer = (Customer)item.Entity; 
              success = changedCustomer.Save(); 
              if (!success) 
                break; 
           } 
       } 
       return success; 
    

    }

See http://msmvps.com/blogs/deborahk/archive/2010/07.aspx