markerasmus
asked on
How do you update using LINQ in the business logic layer?
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.NotSupportedExcept ion: 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
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.NotSupportedExcept
Should I be using multiple data contexts?
Database-Diagram.PNG
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.
Most likely you are using multiple datacontexts already (which causes this) - what you need to do is make sure you are only using one.
ASKER
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.
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.
ASKER
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);
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Naspinki: Thanks, that's actually a very vaild point. I will use that approach.
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