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 8001601
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:08:03+00:00 2026-06-04T16:08:03+00:00

I have a Silverlight RIA app where I share the models and data access

  • 0

I have a Silverlight RIA app where I share the models and data access between the MVC web app and the Silverlight app using compiler directives, and for the server, to see what context I am running under I would check to see if the ChangeSet object was non-null (meaning I was running under RIA rather than MvC). Everything works alright but I had problems with the default code generated by the domain service methods.

Let’s say I had a Person entity, who belonged to certain Groups (Group entity). The Person object has a collection of Groups which I add or remove. After making the changes, the SL app would call the server to persist the changes. What I noticed happening is that the group entity records would be inserted first. That’s fine, since I’m modifying an existing person. However, since each Group entity also has a reference to the existing person, calling AddObject would mark the whole graph – including the person I’m trying to modify – as Added. Then, when the Update statement is called, the default generated code would try to Attach the person, which now has a state of Added, to the context, with not-so-hilarious results.

When I make the original call for an entity or set of entities in a query, all of the EntityKeys for the entities are filled in. Once on the client, then EntityKey is filled in for each object. When the entity returns from the client to be updated on the server, the EntityKey is null. I created a new RIA services project and verified that this is the case. I’m running RIA Services SP1 and I am not using composition. I kind of understand the EntityKey problem – the change tracking done is on two separate contexts. EF doesn’t know about the change tracking done on the SL side. However, it IS passing back the object graph, including related entities, so using AddObject is a problem unless I check the database for the existence of an object with the same key first.

I have code that works. I don’t know how WELL it works but I’m doing some further testing today to see what’s going on. Here it is:

    /// <summary>
    /// Updates an existing object.
    /// </summary>
    /// <typeparam name="TBusinessObject"></typeparam>
    /// <param name="obj"></param>
    protected void Update<TBusinessObject>(TBusinessObject obj) where TBusinessObject : EntityObject
    {
        if (this.ChangeSet != null)
        {

            ObjectStateManager objectStateManager = ObjectContext.ObjectStateManager;
            ObjectSet<TBusinessObject> entitySet = GetEntitySet<TBusinessObject>();
            string setName = entitySet.EntitySet.Name;
            EntityKey key = ObjectContext.CreateEntityKey(setName, obj);
            object dbEntity;

 if (ObjectContext.TryGetObjectByKey(key, out dbEntity) && obj.EntityState == System.Data.EntityState.Detached)
            {
                // An object with the same key exists in the DB, and the entity passed 
                // is marked as detached.
                // Solution: Mark the object as modified, and any child objects need to 
                // be marked as Unchanged as long as there is no Domainoperation.
                ObjectContext.ApplyCurrentValues(setName, obj);
            }

            else if (dbEntity != null)
            {
                // In this case, tryGetObjectByKey said it failed, but the resulting object is
                // filled in, leading me to believe that it did in fact work.
                entitySet.Detach(obj); // Detach the entity
                try
                {
                    ObjectContext.ApplyCurrentValues(setName, obj); // Apply the changes to the entity in DB
                }
                catch (Exception)
                {
                    entitySet.Attach(obj); // Re-attach the entity
                    ObjectContext.ApplyCurrentValues(setName, obj); // Apply the changes to the entity in DB'
                }

            }
            else
            {
                // Add it..? Update must have been called mistakenly.
                entitySet.AddObject(obj);
            }
        }
        else
            DirectInsertUpdate<TBusinessObject>(obj);
    }

Quick walkthrough: If the ChangeSet is null, I’m not under the RIA context, and therefore can call a different method to handle the insert/update and save immediately. That works fine as far as I can tell. For RIA, I generate a key, and see if it exists in the database. If it does and the object I am working with is detached, I apply those values; otherwise, I force detach and apply the values, which works around the added state from any previous Insert calls.

Is there a better way of doing this? I feel like I’m doing way too much work here.

  • 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-06-04T16:08:04+00:00Added an answer on June 4, 2026 at 4:08 pm

    This question has been around with no solid answer, so I’ll tell you what I did… which is nothing. That’s how I handled it in RIA services, using the code above, since I was sharing the RIA client model and the server model.

    After working with RIA services for a year and a half, I’m in the camp that believes that RIA services is good for working with smaller, less complex apps. If you can use [Composite] for your entities, which I couldn’t for many of my entities, then you’re fine.

    RIA services can make throwing together small applications where you want to use the entity from EF really quick, but if you want to use POCOs or you foresee your application getting complex in the future, I would stick with building POCOs on the service end and passing those through regular WCF, and using shared behaviors by making your POCOs partial classes and sharing the behavior code with the client. When you’re trying to create models that work the same on the client and the server, I had to write a ridiculous amount of plumbing code to make it work.

    It definitely IS possible to do, I’ve done it; but there is a lot of hoops you must jump through for everything to work well, and I never fully took into consideration things like your shared model pre-loading lists for use on the client, whereas the server didn’t need these preloaded everytime and actually slowed down the loading of the web page unnecessarily and countering by writing hacky method calls which I had to adopt on the client. (Sorry for the run-on.) The technique I chose to use definitely had its issues.

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

Sidebar

Related Questions

I have a Silverlight 3 app using WCF RIA Services and Entity Framework for
I have a silverlight/RIA service application and in that, after I save the data
I have a Silverlight 4 app which pulls entities down from a database using
I've got a RIA silverlight 4 app with a complex data type as a
Using Silverlight 3 and RIA Services I have the following class defined in my
I have a Silverlight 4 app with RIA services. It's based on Tim Heuer's
I am using EF4/RIA combo in a Silverlight application. I have multiple service methods
In my Silverlight app, after creating ADO.NET Entity Data Model and WCF RIA Services
I am using Silverlight RIA with EF and I have an entity with e.g.
I have the following Solution: SomeProject.Ria (non Silverlight code) SomeProject.Ria.Silverlight (Silverlight light code, namespace

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.