2012-06-11 34 views
3

我正在製作一個.net系統。我正在使用LINQ和MVC。我不得不創建實時數據庫,並希望這可以順利運行,但事實並非如此。ASP.NET LINQ SQL數據庫

新的SQL Server - 微軟的Windows NT 5.0(2195)/ 8.00.760

我創造了誰可以添加/編輯/刪除管理員用戶。基本上,如果我嘗試添加(.InsertOnSubmit)或刪除(.DeleteOnSubmit)任何行,但我不編輯時收到以下錯誤。

"Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the security configuration for MSDTC using the Component Services Administrative tool." 

我GOOGLE了這個錯誤,並發現人認爲它跟「MSDTC服務」,然而,這似乎對服務進行勾選。我已通過SQL Server Management登錄到數據庫,並且此用戶可以添加/刪除。

一個例子:

Controller 
if (_service.AddAccess(access)) return RedirectToAction("Access"); 

public Repository() 
{ 
    _db = new DataClassDataContext(ConfigurationManager.ConnectionStrings["RegistrarsServices"].ConnectionString); 
} 

public void Save() 
{ 
    _db.SubmitChanges(); 
} 

public bool AddAccess(Access access) 
{ 
    try 
    { 
     using (var scope = new TransactionScope()) 
     { 
      _db.Accesses.InsertOnSubmit(access); 
      Save(); 

      scope.Complete(); 
     } 
     return true; 
    } 
    catch (Exception) 
    { 
     return false; 
    } 
} 

*請注意,這個使用開發服務器時所做的工作。微軟的Windows NT 5.2(3790)/ 10.0.1600.22

控制器

private readonly ServiceAdministration _service = new ServiceAdministration(); 

public ActionResult AddAccess() 
{ 
    return View(new EmailViewModel()); 
} 

[HttpPost] 
public ActionResult AddAccess(EmailViewModel emailViewModel) 
{ 
    if (emailViewModel.Button == "Back") return RedirectToAction("Access"); 

    if (!ModelState.IsValid) return View(emailViewModel); 

    Access access = new Access(); 
    access.emailAddress = emailViewModel.emailAddress; 

    if (_service.AddAccess(access)) return RedirectToAction("Access"); 

    emailViewModel.errorMessage = "An error has occurred whilst trying to grant access. Please try again later."; 
    return View(emailViewModel); 
} 

服務

readonly Repository _repository = new Repository(); 

public bool AddAccess(Access access) 
{ 
    return _repository.AddAccess(access); 
} 

真的不知道我錯過了什麼。

在此先感謝您的幫助。

克萊爾:-)

回答

5

你有什麼想法爲什麼你觸發分佈式事務?這是你需要調查的。通常的罪魁禍首是來自單個TransactionScope的多個ADO.Net連接。見ADO.NET and System.TransactionsADO.NET and LINQ to SQL。確保在事務範圍內使用單個連接(即LINQ2SQL上下文)。無論如何,您不應該每個HTTP調用都使用多個

+0

其實我沒有。在添加之前,控制器不會再打另一個查詢。也不確定爲什麼只有在關於分佈式事務的增加/刪除操作時它纔會在編輯時發生。對不起,如果我聽起來很愚蠢,現在才真正學習這一點。它也在開發服務器上工作。 – ClareBear

+0

您確定調用存儲庫的代碼中沒有其他外部TransactionScope?內部作用域然後將簡單地註冊到外部作用域中,而不是真正提交。 –

+0

我確定'AddAccess'功能。請參閱更新後的問題,其中包含代碼 – ClareBear