2015-06-23 20 views
2

我在嘗試創建用戶時遇到錯誤。我在下面的代碼中通過line 15得到它。ObjectDisposedException在創建用戶時

1 public async void AddOrganisationAdmin() 
2 { 
3   
4  String OrganisationName = Request["OrganisationName"]; 
5  Debug.Print(OrganisationName); 
6  RegisterViewModel model = new RegisterViewModel(); 
7  model.Email = "[email protected]" + OrganisationName; 
8  model.Organisation = OrganisationName; 
9  model.Password = "adminPassword!1"; 
10  model.ConfirmPassword = "adminPassword!1"; 
11  model.Role = "Organisation Administrator"; 
12 
13 
14  var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, Organisation = model.Organisation, Role = model.Role }; 
15  IdentityResult result = await UserManager.CreateAsync(user, model.Password); 
16 } 

的錯誤是:

An exception of type 'System.ObjectDisposedException' occurred in mscorlib.dll but 
was not handled in user code 

Additional information: Cannot access a disposed object. 

什麼對象被設置?這些行是asp.net mvc 5項目中的默認註冊函數的副本,所以我做錯了什麼?

我打電話從Ajax帖子的功能是這樣的:

$.ajax({ 
     url: "/Account/AddOrganisationAdmin", 
     type: 'POST', 
     data: { OrganisationName : "@CompanyName"}, 
     success: function (data, textStatus, xhr) { 
      console.log(data); 
     }, 
     error: function (xhr, textStatus, errorThrown) { 
      console.log(xhr); 
     } 
}); 

我進入功能和我有正確的名稱。

+0

你有你自己的'IDisposable'或finalizier的實現嗎? –

+0

@AmitKumarGhosh不,我沒有自己的實現。 –

+0

你能展示''CreateAsync'方法嗎? –

回答

0

tacos_tacos_tacos的評論讓我考慮的DbContext有點過了試錯期間我得到了以下

1 public void AddOrganisationAdmin() 
2 { 
3   
4  String OrganisationName = Request["OrganisationName"]; 
5  Debug.Print(OrganisationName); 
6  RegisterViewModel model = new RegisterViewModel(); 
7  model.Email = "[email protected]" + OrganisationName + ".com"; 
8  model.Organisation = OrganisationName; 
9  model.Password = "adminPassword!1"; 
10  model.ConfirmPassword = "adminPassword!1"; 
11  model.Role = "Organisation Admin"; 
12  Promato.Models.ApplicationDbContext dbcontext = new Promato.Models.ApplicationDbContext(); 
13   
14  var user = new ApplicationUser() { UserName = model.Email, Email = model.Email, Organisation = model.Organisation, Role = model.Role }; 
15  user.PasswordHash = UserManager.PasswordHasher.HashPassword(model.Password); 
16  user.SecurityStamp = Guid.NewGuid().ToString(); 
17  dbcontext.Users.Add(user); 
18  dbcontext.SaveChanges(); 
19 } 

此代碼爲我工作。用戶被添加到.mdf,我可以登錄:)

相關問題