title: System.Data.DataException error occurred in EntityFramework.dll
body: <p>I have been following this tutorial <a href="http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application" rel="noreferrer">Getting Started with Entity Framework 6 Code First using MVC 5</a>.</p>
  
  <p>When I got to the section where I need to run the program and click on Student so that the program would create database I get the following error. </p>
  
  <p><img src="https://i.stack.imgur.com/OtOox.png" alt="enter image description here"></p>
  
  <blockquote>
    <p>System.Data.DataException was unhandled by user code
      HResult=-2146233087
      Message=An exception occurred while initializing the database. See the InnerException for details.
      Source=EntityFramework
      StackTrace:
           at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
           at System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization()
           at System.Data.Entity.Internal.LazyInternalContext.b__4(InternalContext c)
           at System.Data.Entity.Internal.RetryAction<code>1.PerformAction(TInput input)
           at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action</code>1 action)
           at System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase()
           at System.Data.Entity.Internal.InternalContext.Initialize()
           at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
           at System.Data.Entity.Internal.Linq.InternalSet<code>1.Initialize()
           at System.Data.Entity.Internal.Linq.InternalSet</code>1.GetEnumerator()
           at System.Data.Entity.Infrastructure.DbQuery<code>1.System.Collections.Generic.IEnumerable&lt;TResult&gt;.GetEnumerator()
           at System.Collections.Generic.List</code>1..ctor(IEnumerable<code>1 collection)
           at System.Linq.Enumerable.ToList[TSource](IEnumerable</code>1 source)
           at ContosoUniversityFollow.Controllers.StudentController.Index() in c:\Users\Office\Documents\Visual Studio 2013\Projects\ContosoUniversityFollow\ContosoUniversityFollow\Controllers\StudentController.cs:line 21
           at lambda_method(Closure , ControllerBase , Object[] )
           at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
           at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary<code>2 parameters)
           at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary</code>2 parameters)
           at System.Web.Mvc.Async.AsyncControllerActionInvoker.ActionInvocation.InvokeSynchronousActionMethod()
           at System.Web.Mvc.Async.AsyncControllerActionInvoker.b__36(IAsyncResult asyncResult, ActionInvocation innerInvokeState)
           at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult<code>2.CallEndDelegate(IAsyncResult asyncResult)
           at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResultBase</code>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.b__3c()
           at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.&lt;>c__DisplayClass45.b__3e()
      InnerException: System.Data.Entity.Core.EntityException
           HResult=-2146233087
           Message=The underlying provider failed on Open.
           Source=EntityFramework
           StackTrace:
                at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
                at System.Data.Entity.Core.Objects.ObjectContext.EnsureConnection()
                at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func<code>1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)
                at System.Data.Entity.Core.Objects.ObjectQuery</code>1.&lt;>c__DisplayClassb.b__9()
                at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func<code>1 operation)
                at System.Data.Entity.Core.Objects.ObjectQuery</code>1.GetResults(Nullable<code>1 forMergeOption)
                at System.Data.Entity.Core.Objects.ObjectQuery</code>1..GetEnumerator>b__0()
                at System.Lazy<code>1.CreateValue()
                at System.Lazy</code>1.LazyInitValue()
                at System.Lazy<code>1.get_Value()
                at System.Data.Entity.Internal.LazyEnumerator</code>1.MoveNext()
                at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable<code>1 source)
                at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.&lt;GetElementFunction&gt;b__1[TResult](IEnumerable</code>1 sequence)
                at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.ExecuteSingle[TResult](IEnumerable<code>1 query, Expression queryRoot)
                at System.Data.Entity.Core.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
                at System.Data.Entity.Internal.Linq.DbQueryProvider.Execute[TResult](Expression expression)
                at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable</code>1 source)
                at System.Data.Entity.Internal.EdmMetadataRepository.QueryForModelHash(Func<code>2 createContext)
                at System.Data.Entity.Internal.InternalContext.QueryForModelHash()
                at System.Data.Entity.Internal.ModelCompatibilityChecker.CompatibleWithModel(InternalContext internalContext, ModelHashCalculator modelHashCalculator, Boolean throwIfNoMetadata)
                at System.Data.Entity.Internal.InternalContext.CompatibleWithModel(Boolean throwIfNoMetadata)
                at System.Data.Entity.Database.CompatibleWithModel(Boolean throwIfNoMetadata)
                at System.Data.Entity.DropCreateDatabaseIfModelChanges</code>1.InitializeDatabase(TContext context)
                at System.Data.Entity.Internal.InternalContext.&lt;>c__DisplayClasse<code>1.&lt;CreateInitializationAction&gt;b__d()
                at System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action)
           InnerException: System.Data.SqlClient.SqlException
                HResult=-2146232060
                Message=Cannot open database "ContosoUniversity1" requested by the login. The login failed.
    Login failed for user 'Office-PC\Office'.
                Source=.Net SqlClient Data Provider
                ErrorCode=-2146232060
                Class=11
                LineNumber=65536
                Number=4060
                Procedure=""
                Server=(LocalDb)\v11.0
                State=1
                StackTrace:
                     at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal&amp; connection)
                     at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource</code>1 retry, DbConnectionOptions userOptions, DbConnectionInternal&amp; connection)
                     at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource<code>1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal&amp; connection)
                     at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource</code>1 retry, DbConnectionOptions userOptions)
                     at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource<code>1 retry, DbConnectionOptions userOptions)
                     at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource</code>1 retry)
                     at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource<code>1 retry)
                     at System.Data.SqlClient.SqlConnection.Open()
                     at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.&lt;&gt;c__DisplayClass1.&lt;Execute&gt;b__0()
                     at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func</code>1 operation)
                     at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
                     at System.Data.Entity.Core.EntityClient.EntityConnection.Open()
                InnerException: </p>
  </blockquote>
  
  <p>I have a hard time trying to fix the problem. I follow everything and even redo it just to make sure I got it right. I don't think I miss any steps. But there is something wrong. If I run the whole sample code, everything works fine. </p>
  
  <p>Any help is appreciated. </p>
  
  <p>Thanks,</p>
  
  <p>Pheap</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>It was really frustrating for a simple thing that lead to an error like this.
  @mmeasor, I just saw that error as well. It cannot connect to database. So that lead me to check the connection. </p>
  
  <p>What happen was that at first I used this connection string: 
  <img src="https://i.stack.imgur.com/SfocE.png" alt="enter image description here"></p>
  
  <p>So EntityFramework created a database file ContosoUniversity1.mdf in here like C:\Users\Office\ContosoUniversity1.mdf. But then I want it to create another one and use that instead and this time I want it to be in the program App_Data\ folder instead.</p>
  
  <p>So I modified the web.config file to: 
  <img src="https://i.stack.imgur.com/DqhXX.png" alt="enter image description here"></p>
  
  <p>I thought if I do that, EF will create a new database for me in the \App_Data\ folder and link to that instead of the old on in C:\Users\Office. </p>
  
  <p>I was so wrong! that was when I start getting the error posted above. And I cannot find what causes the problem. </p>
  
  <p>With the second web.config, I tried to copy the database file to the App_Data folder, but that still doesn't work. </p>
  
  <p>Now I use the first connection string and it works fine. </p>
  
  <p>So now the question is: How to make the program links to database file in the \App_Data\ folder instead?</p>
  
  <p>Thanks,</p>
  
  <p>Pheap</p>