Solved

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

Posted on 2010-08-20
6
453 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

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 500 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

Business Impact of IT Communications

What are the business impacts of how well businesses communicate during an IT incident? Targeting, speed, and transparency all matter. Find out more in this infographic.

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

739 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