0

I am getting this error in my recently deployed MVC 4 web application, whenever I click a link that requires database access:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

When I run the mvc app locally, through visual studio, connecting to these remote databases, I do not get this error, and the credentials are correct because when I "Connect to database" it logs in and retrieves all the entities as normal. I published the site to the server's FTP, as the host does not have a webdeploy service.

Can anyone explain why my deployed app would have issues with the database and not my local version?

UPDATE

Before I exposed this NullReferenceException, it was a ReflectionTypeLoadException. Don't know if that means anything, but here:

[ReflectionTypeLoadException: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.] 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(Assembly a) +11 System.Linq.d_142.MoveNext() +234 System.Collections.Generic.List1.InsertRange(Int32 index, IEnumerable1 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.RetryLazy2.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.InternalSet1.Initialize() +52 System.Data.Entity.Internal.Linq.InternalSet1.get_InternalContext() +15 System.Data.Entity.Infrastructure.DbQuery1.System.Linq.IQueryable.get_Provider() +37 System.Linq.Queryable.Select(IQueryable1 source, Expression1 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, IDictionary2 parameters) +182 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) +27 System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +28 System.Web.Mvc.Async.<>c__DisplayClass81.b__7(IAsyncResult ) +10 System.Web.Mvc.Async.WrappedAsyncResult1.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.WrappedAsyncResult1.End() +50 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34 System.Web.Mvc.Async.<>c_DisplayClass2a.b_20() +24 System.Web.Mvc.Async.<>c_DisplayClass25.b_22(IAsyncResult asyncResult) +99 System.Web.Mvc.Async.WrappedAsyncResult1.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.WrappedAsyncResult1.End() +55 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +39 System.Web.Mvc.Async.<>c_DisplayClass4.b__3(IAsyncResult ar) +23 System.Web.Mvc.Async.WrappedAsyncResult1.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.WrappedAsyncResult1.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& completedSynchronously) +155

Update

Ok I just attempted to deploy a brand new MVC app from Visual Studio, and that gave me no errors when trying to access models (I made a simple Students model and created a new "Student" in the database, something that would not work in my actual MVC app. Any ideas?

barnacle.m
  • 2,070
  • 3
  • 38
  • 82
  • The error you are getting doesn't really sounds like an EF error - A NullReference means your code is trying to operate on an object, but the object is null. – StanK Aug 15 '13 at 21:13
  • If you edit the web.config on the deployed site to show remote errors, and make sure that the .pdb file is uploaded as well, you might get a better idea of where this is happening. You should also consider using [Elmah](https://code.google.com/p/elmah/) (it is on Nuget) as this is a great error logging tool for live sites. – StanK Aug 15 '13 at 21:16
  • I should add that the error that I got before exposing this error with some code was this: Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information – barnacle.m Aug 15 '13 at 21:34

1 Answers1

0

The table in the database might not exist. I am facing the same problem now, the easiest way is to delete the whole DB and let EF recreate everything. If you already have some sensitive data, then it is trickier. And you might try these too:

Database.SetInitializer(new DropCreateDatabaseIfModelChanges<YourDbContext>());
//  or you can use DropCreateDatabaseAlways<>

From here: EF4 Code First create new table
Or you can check this one: Entity Framework 4.1 Code First not creating tables

Community
  • 1
  • 1
Gábor Imre
  • 5,899
  • 2
  • 35
  • 48
  • Nope, the tables are there, I've checked. They were created through entity framework from the beginning. And i've just realised that if I execute a SQL Command in c#, bypassing the use of the database context class, it reads the database. Something strange happening with the use of the context class. – barnacle.m Aug 16 '13 at 10:52