Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 798089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:54:54+00:00 2026-05-14T22:54:54+00:00

I’m sharing data via RIA services using a presentation model on top of LINQ

  • 0

I’m sharing data via RIA services using a presentation model on top of LINQ to SQL classes. On the Silverlight client, I created a couple of new entities (album and artist), associated them with each other (by either adding the album to the artist’s album collection, or setting the Artist property on the album – either one works), added them to the context, and submitted changes.

On the server, I get two separate Insert calls – one for the album and one for the artist. These entitites are new so their ID values are both set to the default int value (0 – keep in mind that depending on my DB, this could be a valid ID in the DB) because as far as I know you don’t set IDs for new entities on the client. This all would work fine if I was transferring the LINQ to SQL classes via my RIA services, because even though the Album insert includes the Artist and the Artist insert includes the Album, both are Entities and the L2S context recognizes them. However, with my custom presentation model objects, I need to convert them back to the LINQ to SQL classes maintaining the associations in the process so they can be added to the L2S context.

Put simply, as far as I can tell, this is impossible. Each entity gets its own Insert call, but there’s no way you can just insert the one entity because without IDs the associations are lost. If the database used GUID identifiers it would be a different story because I could set those on the client.

Is this possible, or should I be pursuing another design?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:54:55+00:00Added an answer on May 14, 2026 at 10:54 pm

    If you create the correct parent-child associations, you’ll just need to track the inserted presentation model(PM)-entity relationships:

    PM’s:

    public class Parent
    {
        [Key]
        public int? ParentID { get; set; }
    
        [Include]
        [Composition]
        [Association("Parent_1-*_Child", "ParentID", "ParentID", IsForeignKey = false)]
        public IEnumerable<Child> Children { get; set; }
    }
    
    public class Child
    {
        [Key]
        public int? ChildID { get; set; }
    
        [Include]
        [Association("Parent_1-*_Child", "ParentID", "ParentID", IsForeignKey = true)]
        public Parent Parent { get; set; }
    }
    

    Be sure to use [Composition] to force WCF RIA to call the InsertChild method on the DomainService.

    Silverlight:

    ...
    public Child NewChild(Parent parent)
    {
        return new Child
                    {
                        ParentID = parent.ParentID,
                        Parent = parent,
                    };
    }
    ...
    public void SubmitChanges()
    {
        DomainContext.SubmitChanges(SaveComplete, null);
    }
    ...
    

    If the Parent is not new, it will have a ParentID. If it is new, the Parent ID will be null. By setting the Child.Parent to the reference of the new Parent, RIA understands what you are trying to do preserves the reference after it has been sent to the server.

    DomainService on the server:

    [EnableClientAccess]
    public class FamilyDomainService : DomainService
    {
        private readonly IDictionary<object, EntityObject> _insertedObjectMap;
    
        public void InsertParent(Parent parent)
        {
            ParentEntity parentEntity = new ParentEntity();
    
            ObjectContext.AddToParents(parentEntity);
            _insertedObjectMap[parent] = parentEntity;
    
            ChangeSet.Associate(parent, parentEntity, (p, e) => p.ParentID = e.ParentID;
        }
    
        public void InsertChild(Child child)
        {
            var childEntity = new ChildEntity();
    
            if (child.ParentID.HasValue) // Used when the Parent already exists, but the Child is new
            {
                childEntity.ParentID = child.ParentID.GetValueOrDefault();
                ObjectContext.AddToChildren(childEntity);
            }
            else // Used when the Parent and Child are inserted on the same request
            {
                ParentEntity parentEntity;
                if (child.Parent != null && _insertedObjectMap.TryGetValue(child.Parent, out parentEntity))
                {
                    parentEntity.Children.Add(childEntity);
                    ChangeSet.Associate(child, childEntity, (c, e) => c.ParentID = e.Parent.ParentID);
                }
                else
                {
                    throw new Exception("Unable to insert Child: ParentID is null and the parent Parent cannot be found");
                }
            }
    
            _insertedObjectMap[child] = childEntity;
    
            ChangeSet.Associate(child, childEntity, (c, e) => c.ChildID = e.ChildID );
        }
    
        protected override bool PersistChangeSet()
        {
            ObjectContext.SaveChanges();
            _insertedObjectMap.Clear();
            return true;
        }
    }
    

    The two important pieces here. First, the ‘_insertedObjectMap’ stores the relationship between newly inserted entities that do not have the ID set. Since you are doing this in a transaction and single call to the DB, the ID will only be set after all entities have been inserted. By storing the relationship, the Child PM can find the entity version of the Parent PM using the database. The Child entity is added to the Children collection on the Parent entity and LINQToSQL or LINQToEnityFramework should handle the foreign key for you.

    The second piece is associating the changes after the transaction is committed. In the scenario where the Parent and Child are both submitted, you must remember to set the ParentID foreign key on the Child.

    My info from the ChangeSet.Associate() came from: http://blogs.msdn.com/deepm/archive/2009/11/20/wcf-ria-services-presentation-model-explained.aspx

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.