我已經閱讀了很多關於這個錯誤的帖子,大多數人都說它是關於在AppConfig中的connectionstring,它不應該是EF連接字符串,而應該是普通的舊的sql連接字符串。我已經確定了的ConnectionString是正確的,我使用的是SQL連接字符串,但我仍然不斷收到錯誤... 這是代碼實體類型ApplicationUser不是模型的一部分,當添加用戶時引發當前上下文
public void RegisterDealer(RegisterAdministrator model)
{
var user = new ApplicationUser { UserName = model.administratorName, Email = model.administratorEmail, AccountNumber = model.AccountNumber, LoginUserName = model.LoginUserName };
ApplicationDbContext dbc = new ApplicationDbContext();
PasswordHasher MyPasswordHash = new PasswordHasher();
user.PasswordHash = MyPasswordHash.HashPassword(model.administratorPassword);
user.SecurityStamp = Guid.NewGuid().ToString();
user.LockoutEnabled = true;
user.AccountNumber = "1";
user.IsActive = true;
dbc.Users.Add(user);
dbc.SaveChanges();
AdvancewareIdentity ICE = new AdvancewareIdentity();
AspNetUserRole AUR = new AspNetUserRole();
AUR.UserId = user.Id;
// Dealer RoleID
// 8870ccb1-8ced-4703-a6bd-303c5ff5faf7
AUR.RoleId = "8870ccb1-8ced-4703-a6bd-303c5ff5faf7";
ICE.AspNetUserRoles.Add(AUR);
ICE.SaveChanges();
//UserManager.SendEmail(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + id + "\">here</a>");
}
的錯誤是正確的
dbc.Users.Add(user);
堆棧跟蹤是這樣的..
at System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type entityType)
at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
at System.Data.Entity.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)
at System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)
at System.Data.Entity.DbSet`1.Add(TEntity entity)
at AdvanceWareDAL.Authentication.AdministrationAccountController.RegisterDealer(RegisterAdministrator model) in c:\AdvanceWareRevision\AdvanceWareDAL\Authentication\AdministrationAccountController.cs:line 306
at AdvanceWareDAL.Authentication.OQOEDAL.AddNewDealer(NewDealer dealer) in c:\AdvanceWareRevision\AdvanceWareDAL\Models\OQOEDAL.cs:line 599
at AdvanceWareDAL.OQOEAdminstrationDAL.AddNewDealerFromAdministration(NewDealer dealer, String loggedinuser) in c:\AdvanceWareRevision\AdvanceWareDAL\OQOEAdminstrationDAL.cs:line 96
at AdvanceWareMVC.Controllers.AdministrationController.AddNewDealer(NewDealer dealer) in c:\AdvanceWareRevision\AdvanceWareMVC\Controllers\AdministrationController.cs:line 133
at lambda_method(Closure , ControllerBase , Object[])
at System.Web.Mvc.ActionMethodDispatcher.<>c__DisplayClass1.<WrapVoidAction>b__0(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase`1.End()
at System.Web.Mvc.Async.AsyncResultWrapper.End[TResult](IAsyncResult asyncResult, Object tag)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
有誰看到,我失去了一些東西或一個想法在尋找什麼,在解決這個問題?
編輯
這裏是通過的UserManager的的DbContext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("AdvancewareIdentity", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
你可以發佈ApplicationDbContext的代碼嗎? – Lidaranis
您的ApplicationDbContext是否繼承IdentityDbContext? –
@Lidaranis,我添加了DbContext代碼 – Chris