Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-08-20
6
Medium Priority
?
457 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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Suggested Courses

877 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