Linq Update diversi Datacontext

venerdì 26 marzo 2010 - 13.07

mtugnoli Profilo | Newbie

Sono alle prese con un errore che non riesco propio a risolvere....

Questo e' il mio sorgente

[i]
protected static ArticlesDataContext GetContext()
{
return new ArticlesDataContext(Settings.ConnectionString);
}

public static bool InsertCategory(tbh_Category category)
{
category.AddedDate = DateTime.Now;
category.AddedBy = CurrentUserName;

bool success = false;

try
{
using (ArticlesDataContext context = GetContext())
{
context.tbh_Categories.InsertOnSubmit(category);
context.SubmitChanges();
success = true;
}
}
catch
{
success = false;
}

return success;
}

public static bool UpdateCategory(tbh_Category changedCategory)
{
bool success = false;

try
{
using (ArticlesDataContext context = GetContext())
{
context.tbh_Categories.Attach(changedCategory, true);
context.SubmitChanges();
}
success = true;
}
catch
{
success = false;
}

return success;
}



protected void UpdateCategory()
{
tbh_Category lCategory;

if (CategoryId > 0)
{
lCategory = tbh_Category.GetCategoryByID(CategoryId);
}
else
{
lCategory = new tbh_Category();
}

lCategory.Title = txtTitle.Text;
lCategory.Importance = int.Parse(txtImportance.Text);
lCategory.Description = txtDescription.Text;

lCategory.ImageUrl = GetItemImage(fuImageURL, txtImageUrl);
lCategory.Active = cbActive.Checked;

lCategory.UpdatedBy = UserName;
lCategory.UpdatedDate = DateTime.Now;

if (lCategory.CategoryID == 0)
{
lCategory.Active = true;
lCategory.AddedBy = UserName;
lCategory.AddedDate = DateTime.Now;
}

bool bRet = false;

if (CategoryId > 0)
bRet = tbh_Category.UpdateCategory(lCategory);
else
bRet = tbh_Category.InsertCategory(lCategory);[/i]


Il problema e' il seguente, ho una pagina che gestisce sia l'inserimento che la modifica di un record,
in fase di inserimento si richiama la funzione InsertCategory ed e' tutto 0k,
in fase di aggiornamento con la funzione UpdateCategory si verifica il seguente errore :

[b][i]An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext[/i] [/b]

Ho fatto varie ricerche e sembra che linq non possa aggiornaqre tra 2 Datacontext diversi, propongono varie soluzioni, ma non sono riuscito a capirle e a riprodurle.

Mi potete aiutare ?


Grazie

Marco
Partecipa anche tu! Registrati!
Hai bisogno di aiuto ?
Perchè non ti registri subito?

Dopo esserti registrato potrai chiedere
aiuto sul nostro Forum oppure aiutare gli altri

Consulta le Stanze disponibili.

Registrati ora !
Copyright © dotNetHell.it 2002-2024
Running on Windows Server 2008 R2 Standard, SQL Server 2012 & ASP.NET 3.5