Solved

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

Posted on 2010-08-20
6
454 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

635 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