[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.0

How to perform Insert/Update/Delete via LINQ to Entities (Entity Framework) across Many-to-Many or 1-to-Many relation (using ADO.NET Data Service)?

Asked by robbersrow in WPF and Silverlight, SQL Server 2008, Language Integrated Query - LINQ

Tags: Silverlight, C#, LINQ, ADO.NET Data Services, Entity Framework, SQL Server, all

I have a Silverlight application which uses ADO.NET Data Services and the Entity Framework to interact with a SQL Server Database.  In the SQL Server database I have:  User, Role, and User_Role (join) tables. I successfully use the ADO.NET Entity Framework Wizard to generate the Model from the database in my Silverlight application server-side code (.NET), and the resulting EDMX diagram properly shows a many-to-many relationship between the User and Role table.  So this part is working ok.

Then my Silverlight/C# client-side code is set up to where I have a Service Reference (Proxy) to the server-side ADO.NET Data Service which accesses the Entity Framework EDMX definitions (also on the server-side, along with the database).

I am able to successfully create a User or a Role to the database.  The problem comes when I try to ADD ROLES TO A USER, and THEN save that user to the database.

First, here is an example of what DOES work in my Silverlight C# code:

Proxy.Entities proxy = new Proxy.Entities(new Uri("DataService.svc", UriKind.Relative));

Proxy.User user = new Proxy.User();
user.Username = UsernameTB.Text;
user.FirstName = FirstNameTB.Text;
user.LastName = LastNameTB.Text;
user.CreatedDate = DateTime.Now;
user.UpdatedDate = DateTime.Now;
proxy.AddObject("User", user);
proxy.BeginSaveChanges(

    SaveChangesOptions.None,

    (asyncResult) => { proxy.EndSaveChanges(asyncResult); },

    null

);

Now, here is an example of what DOES NOT work (note the lines where I say "selectedUser.Role.Add( )", and also note that "selectedUser" is a user that is already in the database and has been successfully queried/retrieved in another part of the client-side code):

public UpdateUserRoles(Proxy.User selectedUser, List<Proxy.Role> roles)
{

    Proxy.Entities proxy = new Proxy.Entities(new Uri("DataService.svc", UriKind.Relative));

    proxy.AttachTo("User", selectedUser);

    selectedUser.Role.Add(roles[0]);

    selectedUser.Role.Add(roles[1]);

    selectedUser.Role.Add(roles[2]);

    proxy.UpdateObject(selectedUser);
    proxy.BeginSaveChanges(

        SaveChangesOptions.Batch,

        (asyncResult) => { proxy.EndSaveChanges(asyncResult); },

        null

    );

}

In this second example, I would expect the "User_Role" join table in the database to be automatically updated with the new User/Role entries (corresponding to adding specific Roles to a User).  In other words, there should be a new ROW in the User_Role join database table for every new Role I add to a User.  But for some reason, the User_Role join table does not get updated accordingly.  It remains blank, without any new ROWs added.  Furthermore, the same issue applies to tables which are related via a 1-to-many relationship.  Please let me know if you see something that I may be missing here.  Any help would be greatly appreciated.  Thank you.
[+][-]10/16/08 02:34 AM, ID: 22729126Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/16/08 11:10 AM, ID: 22734031Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/17/08 10:51 AM, ID: 22743373Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/17/08 11:46 AM, ID: 22743903Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/17/08 12:04 PM, ID: 22744121Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10/17/08 05:09 PM, ID: 22746459Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10/19/08 11:47 AM, ID: 22753266Assisted Solution

Assisted solutions are selected by the member who asked the question as a comment that contributed to their question's solution.

Start your 30-day free trial to view this Assisted Solution or ask the Experts your question.

 
[+][-]10/19/08 01:33 PM, ID: 22753630Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: WPF and Silverlight, SQL Server 2008, Language Integrated Query - LINQ
Tags: Silverlight, C#, LINQ, ADO.NET Data Services, Entity Framework, SQL Server, all
Sign Up Now!
Solution Provided By: robbersrow
Participating Experts: 1
Solution Grade: A
 
[+][-]10/19/08 01:33 PM, ID: 22753631Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89 - Hierarchy / EE_QW_2_20070628