Solved

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

Posted on 2010-08-20
6
448 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:markerasmus
Comment Utility
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
Comment Utility
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
Comment Utility
Naspinki:  Thanks, that's actually a very vaild point.  I will use that approach.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now