Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do you update using LINQ in the business logic layer?

Posted on 2010-08-20
6
Medium Priority
?
456 Views
Last Modified: 2013-11-11
I am trying to update using LINQ that encapsulates the business logic in a separate layer.  This is working fine with SELECT, INSERT and DELTE LINQ queries, but UPDATE statements are proving a bit of a headache.  

I have tried using the db.Attach method but that is failing when there is more that one table being updated, which is pretty much 99% of the time.

The error I am getting is: "System.NotSupportedException: An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext. This is not supported."

Should I be using multiple data contexts?  
Database-Diagram.PNG
0
Comment
Question by:markerasmus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 18

Expert Comment

by:Anil Golamari
ID: 33487369
http://geekswithblogs.net/michelotti/archive/2007/12/27/118022.aspx

Take a look at the above link which may give you some idea about fixing the error. And why you have two relationships between Team and MatchTable
0
 
LVL 21

Expert Comment

by:naspinski
ID: 33488691
Can you show us the code that is being called?

Most likely you are using multiple datacontexts already (which causes this) - what you need to do is make sure you are only using one.
0
 

Author Comment

by:markerasmus
ID: 33490813
Luck85 - I already did some research before posting this thread and I came across http://geekswithblogs.net/michelotti/archive/2007/12/27/118022.aspx, which is why I asked whether I should be using multiple data contexts.  I just don't understand why it works with one data context if you combine the business logic within the presentation layer.  
By combining the presentation layer (i.e. having the LINQ in the aspx.cs or ascx.cs) the update works by creating an instance of the orginal Match object, making changes to it, and simply calling db.SubmitChanges().  However when separting the presentation layer and business logic I need to use the db.Attach method, and this is where the confusion starts.

The reason there are 2 relationships between the Team and Match table is because there are 2 teams (TeamA and TeamB) in the match table that reference the Team table.
0
A new era in Cloud training has arrived.

A day that will go down in Cloud history.. But are you ready for it? Will you accept this Cloud challenge?

 

Author Comment

by:markerasmus
ID: 33490824
naspinski: The code is at work but I have recreated the basic idea in the code snippet below.  I am not currently using multiple data contexts - only one.
//BLL
public static void UpdateMatch(Match match)
{
	using (TournamentDataContext db = new TournamentDataContext())
	{
		db.Attach(match, GetMatchByID(match.ID));
		dataContext.SubmitChanges();	
	}
}

//Presentation Layer (Pseudo code)
protected void UpdateMatch()
{
	//Instantiate the Match class.
	
	//Makes changes to the new match object.
	
	//Update the match object.
	TournamentBLL.UpdateMatch(match);
			
}

Open in new window

0
 
LVL 21

Accepted Solution

by:
naspinski earned 2000 total points
ID: 33491600
I have to disagree, I think you are using multiple datacontexts, or I do not understand why you are using attach at all (I never use it as it generally is more difficult than the more straight forward ways).

Where are you creating match?  Is it using a datacontext somewhere?

Also, you are doing a lot of stuff in your presentation layer like you said you are trying not to, why not move it all to the DAL?  Instead of:

       //Instantiate the Match class.
      
      //Makes changes to the new match object.
      
      //Update the match object.
      TournamentBLL.UpdateMatch(match);

Why not just have TournamentBLL.UpdateMatch do all that logic (and take in more inputs) since you are really not separating logic, but kind of halfway separating it?
0
 

Author Comment

by:markerasmus
ID: 33495472
Naspinki:  Thanks, that's actually a very vaild point.  I will use that approach.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question