我正在制作一个 .net 系统。我正在使用 LINQ 和 MVC。我必须创建实时数据库并希望它能够顺利运行,但事实并非如此。
新的 SQL SERVER - Microsoft 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管理登录数据库,该用户可以添加/删除。
一个例子:
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
Controller
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);
}
我真的不知道我错过了什么。
预先感谢您的帮助。
克莱尔:-)
最佳答案
您知道为什么要触发分布式事务吗?这是你需要调查的。通常的罪魁祸首是来自单个 TransactionScope 的多个 ADO.Net 连接。请参阅ADO.NET and System.Transactions和 ADO.NET and LINQ to SQL 。确保在事务范围内使用单个连接(即 LINQ2SQL 上下文)。无论如何,每个 HTTP 调用不必使用多个。
关于ASP.NET LINQ SQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10980747/