问题。在注册方案中,我试图在用户表中插入一个用户,然后为该用户调用WebSercurity.CreateAccount
(在一个事务中)。这会导致MS DTC在服务器上不可用的错误。使用具有在TrasactionScope其他查询WebSercurity.CreateAccount,而不DTC启用
Description。我这样做的原因是因为我有一个Customer
实体,它继承自User
,所以WebSercurity.CreateUserAndAccount
不能使用,因为它不知道约Customer
,只是插入User
记录。
我使用Asp.net MVC 4的EntityFramework 5,CodeFirst,和SQL Server 2008 R2。
任何建议不使用DTC将不胜感激!
编辑。 这是显而易见的,为什么这个错误发生,因为websecurity使用它自己的数据库连接,我的仓库使用其他连接,虽然我已经配置simplemembership使用相同DbContext
类作为我的资料库,但问题是它创建了一个在DbContext
的新实例...
我希望如果有通过现有上下文对象,或连接到WebSecurity
它的方法使用的方式。
下面的代码:
if (ModelState.IsValid)
{
//using (TransactionScope tx = new TransactionScope())
//{
UnitOfWork.UserRepository.Insert(new Customer
{
FirstName = model.FirstName,
LastName = model.LastName,
Email = model.Email,
Tel = model.Tel,
Mobile = model.Mobile,
BirthDate = model.BirthDate,
InsertDate = DateTime.Now,
UserType = UserType.Customer,
MaritalStatus = model.MaritalStatus,
ZipCode = model.ZipCode,
StreetAddress = model.StreetAddress,
City = model.City,
State = model.State
});
UnitOfWork.Commit();
string token = WebSecurity.CreateAccount(model.Email, model.Password, true);
Roles.AddUserToRole(model.Email, "Customer");
//WebSecurity.Login(model.Email, model.Password, true);
await Task.Run(() => EmailHelper.SendConfrimationEmail(token, model.Email));
// tx.Complete();
//}
谢谢,是的,我知道,是不是有办法通过连接到'WebSecurity'方法? 你有什么建议会员供应商? – Ashkan 2013-02-22 01:19:54