每当我点击需要访问数据库的链接时,我在最近部署的MVC 4 Web应用程序中收到此错误:
对象引用未设置为对象的实例。
描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。
异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。
当我在本地运行mvc应用程序,通过visual studio,连接到这些远程数据库时,我没有收到此错误,并且凭据是正确的,因为当我“连接到数据库”时,它登录并检索所有实体为正常。我将该站点发布到服务器的FTP,因为主机没有webdeploy服务。
有人可以解释为什么我部署的应用程序会出现数据库问题,而不是我的本地版本吗?
更新
在我暴露这个NullReferenceException之前,它是一个ReflectionTypeLoadException。不知道这是否意味着什么,但在这里:
[ReflectionTypeLoadException:无法加载一个或多个请求的类型。检索LoaderExceptions属性以获取更多信息。]
System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)+0
System.Reflection.RuntimeModule.GetTypes()+ 4
System.Reflection.Assembly.GetTypes()+61
System.Data.Entity.ModelConfiguration.Mappers.TypeMapper。< .ctor> b_ 1(程序集a)+11
System.Linq.d _14 2.MoveNext() +234
System.Collections.Generic.List
1.InsertRange(Int32 index,IEnumerable 1 collection) +392
System.Data.Entity.ModelConfiguration.Mappers.TypeMapper..ctor(MappingContext mappingContext) +258
System.Data.Entity.DbModelBuilder.MapTypes(EdmModel model) +114
System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo) +112
System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +59
System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +62
System.Data.Entity.Internal.RetryLazy
2.GetValue(TInput input)+123
System.Data.Entity.Internal.LazyInternalContext.InitializeContext()+ 461
System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)+18
System.Data.Entity.Internal.Linq.InternalSet 1.Initialize() +52
System.Data.Entity.Internal.Linq.InternalSet
1.get_InternalContext()+15
System.Data.Entity.Infrastructure.DbQuery 1.System.Linq.IQueryable.get_Provider() +37
System.Linq.Queryable.Select(IQueryable
1个来源,表达式1 selector) +66
ProOptInteractive.Controllers.ServiceController.Index() +115
lambda_method(Closure , ControllerBase , Object[] ) +35
System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +14
System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary
2个参数)+182
System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext,ActionDescriptor actionDescriptor,IDictionary 2 parameters) +27
System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +28
System.Web.Mvc.Async.<>c__DisplayClass8
1.b__7(IAsyncResult )+10
System.Web.Mvc.Async.WrappedAsyncResult 1.End() +50
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +58
System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +225
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResult
1.End()+50
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)+34
System.Web.Mvc.Async。&lt;&gt; c _DisplayClass2a.b_ 20()+24
System.Web.Mvc.Async。&lt;&gt; c _DisplayClass25.b_ 22(IAsyncResult asyncResult)+99
System.Web.Mvc.Async.WrappedAsyncResult 1.End() +50
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +14
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult
1.End()+55
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)+39
System.Web.Mvc.Async。&lt;&gt; c _DisplayClass4.b__3(IAsyncResult ar)+23
System.Web.Mvc.Async.WrappedAsyncResult 1.End() +55
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +29
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +25
System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23
System.Web.Mvc.Async.WrappedAsyncResult
1.End()+55
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()+9628700
System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean&amp; completedSynchronously)+155
更新
好的我只是尝试从Visual Studio部署一个全新的MVC应用程序,这在尝试访问模型时没有给我任何错误(我做了一个简单的学生模型并在数据库中创建了一个新的“学生”,这将是不能在我的实际MVC应用程序中工作。任何想法?
答案 0 :(得分:0)
数据库中的表可能不存在。我现在面临同样的问题,最简单的方法是删除整个数据库,让EF重新创建所有内容。如果您已经有一些敏感数据,那就更难了。你也可以尝试这些:
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<YourDbContext>());
// or you can use DropCreateDatabaseAlways<>
从这里开始:EF4 Code First create new table
或者你可以查看这个:Entity Framework 4.1 Code First not creating tables