MS ASP MVC教程EF DB很难

时间:2014-11-25 19:34:54

标签: c# asp.net asp.net-mvc entity-framework

我试图通过ASP.net MVC教程。我已经达到了使用Entity Framework(http://www.asp.net/mvc/overview/getting-started/introduction/accessing-your-models-data-from-a-controller)构建模型的程度。当我启动应用程序并导航到电影页面时,我得到以下异常:

System.Data.SqlClient.SqlException {"CREATE DATABASE permission denied in database 'master'."}

这种情况总是发生,即使我已经按照教程进行了处理。

我想我会尝试通过将connectionString属性设置为:更改web.config的DefaultConnection元素来尝试指向我们的数据库服务器:

"Data Source=XXXXXX\MSSQLSERVER_2012;Initial Catalog=MarkW-ASPMVC-Proj;Integrated Security=false;username=XXXXX;password=XXXXX;"

这对应用程序的状态没有任何影响,我得到了同样的确切异常。我尝试了干净,重建所有,我甚至将DefaultConnection连接字符串节点添加到调试和发布web.config转换,但是当我在运行时检查连接时异常发生时它说:

"Data Source=.\\SQLEXPRESS;Initial Catalog=MvcApplication1.Models.Movie+MovieDBContext;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFrameworkMUE"

这是从哪里来的?我搜索了整个项目,SQLEXPRESS没有出现在连接字符串中。我可能做错的教程是不能以这种方式工作,最重要的是......为什么EF似乎忽略了我在web配置中对默认连接字符串的值?

另一个编辑:我设法为此找到了解决办法...感觉更像是一个黑客。我阅读了我的上下文派生自的DbContext类型的文档。其中一个构造函数接受一个连接字符串,所以我想我可以像这样修改类:

    public class MovieDBContext : DbContext
    {
        public MovieDBContext() : base(System.Configuration.ConfigurationManager.
ConnectionStrings["DefaultConnection"].ConnectionString)
        {

        }
        public DbSet<Movie> Movies { get; set; }
    }

现在可以正常工作......但我仍然想知道为什么我必须这样做,以及为什么EF没有使用web.config值。

完成堆栈跟踪:

[SqlException (0x80131904): CREATE DATABASE permission denied in database 'master'.] System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) +1753986 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction) +5296058 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +558 System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1682 System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout) +280 System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) +405 System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +160 System.Data.SqlClient.<>c__DisplayClassa.<DbCreateDatabase>b__7(SqlConnection conn) +33 System.Data.SqlClient.SqlProviderServices.UsingConnection(SqlConnection sqlConnection, Action1 act) +92 System.Data.SqlClient.SqlProviderServices.UsingMasterConnection(SqlConnection sqlConnection, Action1 act) +384 System.Data.SqlClient.SqlProviderServices.DbCreateDatabase(DbConnection connection, Nullable1 commandTimeout, StoreItemCollection storeItemCollection) +276 System.Data.Objects.ObjectContext.CreateDatabase() +84 System.Data.Entity.Migrations.Utilities.DatabaseCreator.Create(DbConnection connection) +101 System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists() +115 System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists() +34 System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +150 System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update() +33 System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func3 createMigrator, ObjectContext objectContext) +456 System.Data.Entity.Internal.InternalContext.CreateDatabase(ObjectContext objectContext) +173 System.Data.Entity.Database.Create(Boolean skipExistsCheck) +241 System.Data.Entity.CreateDatabaseIfNotExists1.InitializeDatabase(TContext context) +396 System.Data.Entity.Internal.<>c__DisplayClass8.b__6() +67 System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +110 System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +273 System.Data.Entity.Internal.LazyInternalContext.b__4(InternalContext c) +31 System.Data.Entity.Internal.RetryAction1.PerformAction(TInput input) +143 System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action1 action) +270 System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() +123 System.Data.Entity.Internal.InternalContext.Initialize() +42 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +39 System.Data.Entity.Internal.Linq.InternalSet1.Initialize() +137 System.Data.Entity.Internal.Linq.InternalSet1.GetEnumerator() +38 System.Data.Entity.Infrastructure.DbQuery1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() +99 System.Collections.Generic.List1..ctor(IEnumerable1 collection) +369 System.Linq.Enumerable.ToList(IEnumerable1 source) +58 MvcApplication1.Controllers.MovieController.Index() in c:\Users\mwolfert\Documents\Visual Studio 2012\Projects\MvcApplication1\MvcApplication1\Controllers\MovieController.cs:21 lambda_method(Closure , ControllerBase , Object[] ) +101 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +59 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +435 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) +60 System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeSynchronousActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) +50 System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +75 System.Web.Mvc.Async.<>c__DisplayClass81.b__7(IAsyncResult _) +44 System.Web.Mvc.Async.WrappedAsyncResult1.End() +139 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +49 System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +126 System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +323 System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +44 System.Web.Mvc.Async.WrappedAsyncResult1.End() +139 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +102 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +50 System.Web.Mvc.Async.<>c__DisplayClass2a.b__20() +68 System.Web.Mvc.Async.<>c__DisplayClass25.b__22(IAsyncResult asyncResult) +184 System.Web.Mvc.Async.WrappedAsyncResult1.End() +136 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +56 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +40 System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +40 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +47 System.Web.Mvc.Async.WrappedAsyncResult1.End() +151 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +44 System.Web.Mvc.Async.<>c__DisplayClass4.b__3(IAsyncResult ar) +47 System.Web.Mvc.Async.WrappedAsyncResult1.End() +151 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +39 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +39 System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +45 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +47 System.Web.Mvc.Async.WrappedAsyncResult1.End() +151 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59 System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +40 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9658668 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

1 wrapCloseInAction) +1753986 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action

1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) +405 System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +160 System.Data.SqlClient.<>c__DisplayClassa.<DbCreateDatabase>b__7(SqlConnection conn) +33 System.Data.SqlClient.SqlProviderServices.UsingConnection(SqlConnection sqlConnection, Action

0 个答案:

没有答案