2015-10-14 78 views
1

我从文本字段获取值,但找不到未保存在变量中的原因。ASP.NET MVC 5数据为NULL

var user = new ApplicationUser() { UserName = model.Email ,Email = model.Email}; 

usernull尽管电子邮件中包含的值。我通过调试发现它。

{ 
Line 81:  var user = new ApplicationUser() { UserName = model.Email ,Email = model.Email}; 

Line 82:  var result = await UserManager.CreateAsync(user, model.Password); 
Line 83:  if (result.Succeeded) 
Line 84:  { 

82行是错误。 以下是代码。

var user = new ApplicationUser() { UserName = model.Email ,Email = model.Email}; 

var result = await UserManager.CreateAsync(user, model.Password); 
if (result.Succeeded) 
{ 
    var db = new ApplicationDbContext(); 
    var checkingAccount = new CheckingAccount 
    { 
     FirstName = model.FirstName, 
     LastName = model.LastName, 
     AccountNumber = "111014005", 
     Balance = 0, 
     ApplicationUserId = user.Id 

    }; 
    db.CheckingAccounts.Add(checkingAccount); 
    db.SaveChanges(); 

堆栈跟踪:

[Win32Exception (0x80004005): The system cannot find the file specified]

[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)]

System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +414
System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource 1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +78
System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource
1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) +196
System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource 1 retry, DbConnectionOptions userOptions) +146
System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
1 retry, DbConnectionOptions userOptions) +16
System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource 1 retry) +94
System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource
1 retry) +110 System.Data.SqlClient.SqlConnection.Open() +96
System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.b__36(DbConnection t, DbConnectionInterceptionContext c) +10
System.Data.Entity.Infrastructure.Interception.InternalDispatcher 1.Dispatch(TTarget target, Action 2 operation, TInterceptionContext interceptionContext, Action 3 executing, Action 3 executed) +72
System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext) +360
System.Data.Entity.SqlServer.<>c__DisplayClass33.b__32() +426 System.Data.Entity.SqlServer.<>c__DisplayClass1.b__0() +10
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func 1 operation) +189
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation) +78
System.Data.Entity.SqlServer.SqlProviderServices.UsingConnection(DbConnection sqlConnection, Action
1 act) +175
System.Data.Entity.SqlServer.SqlProviderServices.UsingMasterConnection(DbConnection sqlConnection, Action 1 act) +557
System.Data.Entity.SqlServer.SqlProviderServices.CreateDatabaseFromScript(Nullable
1 commandTimeout, DbConnection sqlConnection, String createDatabaseScript) +86
System.Data.Entity.SqlServer.SqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable 1 commandTimeout, StoreItemCollection storeItemCollection) +164
System.Data.Entity.Core.Common.DbProviderServices.CreateDatabase(DbConnection connection, Nullable
1 commandTimeout, StoreItemCollection storeItemCollection) +76
System.Data.Entity.Core.Objects.ObjectContext.CreateDatabase() +134
System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection connection) +119
System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) +142
System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +78
System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func 3 createMigrator, ObjectContext objectContext) +89 System.Data.Entity.Internal.InternalContext.CreateDatabase(ObjectContext objectContext, DatabaseExistenceState existenceState) +116
System.Data.Entity.Database.Create(DatabaseExistenceState existenceState) +218
System.Data.Entity.CreateDatabaseIfNotExists
1.InitializeDatabase(TContext context) +149
System.Data.Entity.Internal.<>c__DisplayClassf 1.<CreateInitializationAction>b__e() +76 System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +60
System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +357 System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c) +7
System.Data.Entity.Internal.RetryAction
1.PerformAction(TInput input) +110 System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action 1 action) +198
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() +73 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +28
System.Data.Entity.Internal.Linq.InternalSet
1.Initialize() +53
System.Data.Entity.Internal.Linq.InternalSet 1.get_InternalContext() +15 System.Data.Entity.Infrastructure.DbQuery 1.System.Linq.IQueryable.get_Provider() +38 System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(IQueryable 1 source, Expression 1 predicate, CancellationToken cancellationToken) +138 System.Data.Entity.QueryableExtensions.FirstOrDefaultAsync(IQueryable 1 source, Expression 1 predicate) +133
Microsoft.AspNet.Identity.EntityFramework.d__6c.MoveNext() +486 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 Microsoft.AspNet.Identity.CultureAwaiter 1.GetResult() +59 Microsoft.AspNet.Identity.<ValidateUserName>d__4.MoveNext() +592 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 Microsoft.AspNet.Identity.<ValidateAsync>d__0.MoveNext() +283 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 Microsoft.AspNet.Identity.<CreateAsync>d__0.MoveNext() +582 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 Microsoft.AspNet.Identity.<CreateAsync>d__d.MoveNext() +531 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52 System.Runtime.CompilerServices.TaskAwaiter
1.GetResult() +24 AutomatedTellerMachine.Controllers.d__9.MoveNext() in e:\Passion\Work\AutomatedTellerMachine\AutomatedTellerMachine\Controllers\AccountController.cs:82 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +93
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +52
System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +84
System.Web.Mvc.Async.<>c__DisplayClass37.b__36(IAsyncResult asyncResult) +17
System.Web.Mvc.Async.WrappedAsyncResult 1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
System.Web.Mvc.Async.AsyncInvocationWithFilters.b__3d() +50 System.Web.Mvc.Async.<>c__DisplayClass46.b__3f() +225 System.Web.Mvc.Async.<>c__DisplayClass33.b__32(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResult 1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
System.Web.Mvc.Async.<>c__DisplayClass2b.b__1c() +26 System.Web.Mvc.Async.<>c__DisplayClass21.b__1e(IAsyncResult asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult 1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.Controller.b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +13
System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +36
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +54
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39 System.Web.Mvc.Controller.b__15(IAsyncResult asyncResult, Controller controller) +12
System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +28
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +54
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.MvcHandler.b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +21
System.Web.Mvc.Async.WrappedAsyncVoid 1.CallEndDelegate(IAsyncResult asyncResult) +36
System.Web.Mvc.Async.WrappedAsyncResultBase
1.End() +54
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +31 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9514812 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

Line 94:      }; 
    Line 95:      db.CheckingAccounts.Add(checkingAccount); 
    Line 96:      db.SaveChanges(); 
    Line 97:      await SignInAsync(user, isPersistent: false); 
    Line 98:      return RedirectToAction("Index", "Home"); 

Entity Validation Error for 96 Line. 
+0

你能否提供一些堆栈跟踪错误? – jgasiorowski

+0

这里'user'没有办法。 – DavidG

+0

先生,我调试过了,用户没有任何价值。这就是为什么第82行给我一个错误。 –

回答

1

堆栈跟踪显示您的应用程序无法连接到数据库:

SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 52 - Unable to locate a Local Database Runtime installation. Verify that SQL Server Express is properly installed and that the Local Database Runtime feature is enabled.)

注意突出显示部分:这表明你不没有安装LocalDb。要么install it要么更改连接字符串以指向存在的数据库。

+0

这是我自动生成的连接字符串。 –

+0

和你的上下文? – DavidG

+0

先生,我不知道上下文。我在MVC 5和Entity Framework上很新。我通过NuGet安装了EF。之后,我开始创建我的项目。与此同时,我对web.config一无所知。数据库也没有。 –