2014-05-01 97 views
0

我是網絡開發新手。我正在嘗試使用MVC 4和EF Code First在下面的URL中給出的Movie DB的MVC教程。MVC 4 CREATE TABLE權限在數據庫中被拒絕

http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/intro-to-aspnet-mvc-4

我創建了互聯網模板一個簡單的項目,並創建了一個模型,模仿我的DB一個實際的表作爲指南給出。然後,我根據現有的數據庫創建了一個表格的新模型,以及問題何時開始。當web.config指向localdb時,一切都正常。

我改變了web.config指向我的實際數據庫,看看代碼如何工作,並由於某些原因,許多用戶發佈,我得到DB權限錯誤​​。這是我的模型。

namespace MVCTest.Models 
{ 
    public class ProjectID 
    { 
     [Key] 
     public int ID{ get; set; } 
     public int TEMP_ID { get; set; } 
     public string STATUS { get; set; } 
     public string DESC{ get; set; } 
    } 
} 

這是我的上下文。控制器是基於此模型和上下文創建的。

using System.Data.Entity; 

namespace MVCTest.Models 
{ 
    public class MVCDBContext : DbContext 
    { 
     // You can add custom code to this file. Changes will not be overwritten. 
     // 
     // If you want Entity Framework to drop and regenerate your database 
     // automatically whenever you change your model schema, add the following 
     // code to the Application_Start method in your Global.asax file. 
     // Note: this will destroy and re-create your database with every model change. 
     // 
     //System.Data.Entity.Database.SetInitializer(new System.Data.Entity.DropCreateDatabaseIfModelChanges<MvcMovie.Models.MvcMovieContext>()); 

     public MVCDBContext() 
      : base("name=MVCDBContext") 
      // : base("Data Source=SERVERNAME;Initial Catalog=DBNAME;Integrated Security=True;") 
     { 
     } 

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

該表已經存在,我只是想對實際SQL表執行RUD操作,而不是在本地數據庫中執行RUD操作。

我已經嘗試了很多選項列出在stackoverflow和許多其他網站,沒有爲我工作。我剩下的唯一選擇是通過EF Data First,但我想看看是否有人有建議,然後我放棄這個項目並轉到該項目。

這裏是堆棧跟蹤:

Exception Details: System.Data.SqlClient.SqlException: CREATE TABLE permission denied in database 'DBNAME'. 

Source Error: 

Line 19:   public ActionResult Index() 
Line 20:   { 
Line 21:    return View(db.ProjectIDs.ToList()); 
Line 22:   } 
Line 23: 

Stack Trace: 

[SqlException (0x80131904): CREATE TABLE permission denied in database 'DBNAME'.] 
    System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +1754082 
    System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5295874 
    System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +242 
    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(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) +405 
    System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +160 
    System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement) +224 
    System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement) +55 
    System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements) +169 
    System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements) +42 
    System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, XDocument targetModel, IEnumerable`1 operations, Boolean downgrading, Boolean auto) +1025 
    System.Data.Entity.Migrations.DbMigrator.AutoMigrate(String migrationId, XDocument sourceModel, XDocument targetModel, Boolean downgrading) +320 
    System.Data.Entity.Migrations.Infrastructure.MigratorBase.AutoMigrate(String migrationId, XDocument sourceModel, XDocument targetModel, Boolean downgrading) +64 
    System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) +538 
    System.Data.Entity.Migrations.Infrastructure.MigratorBase.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) +59 
    System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +897 
    System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update() +33 
    System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func`3 createMigrator, ObjectContext objectContext) +456 
    System.Data.Entity.Internal.InternalContext.CreateDatabase(ObjectContext objectContext) +173 
    System.Data.Entity.Database.Create(Boolean skipExistsCheck) +241 
    System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context) +396 
    System.Data.Entity.Internal.<>c__DisplayClass8.<PerformDatabaseInitialization>b__6() +67 
    System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +110 
    System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +273 
    System.Data.Entity.Internal.LazyInternalContext.<InitializeDatabase>b__4(InternalContext c) +31 
    System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) +143 
    System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 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.InternalSet`1.Initialize() +137 
    System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() +38 
    System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() +99 
    System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +369 
    System.Linq.Enumerable.ToList(IEnumerable`1 source) +58 
    MVCtest.Controllers.ProjectIDsController.Index() in c:\Users\n228322\Data\MVCtest\MVCtest\Controllers\ProjectIDsController.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, IDictionary`2 parameters) +435 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +60 
    System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeSynchronousActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +50 
    System.Web.Mvc.Async.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() +75 
    System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +44 
    System.Web.Mvc.Async.WrappedAsyncResult`1.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.WrappedAsyncResult`1.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.<BeginInvokeAction>b__20() +68 
    System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +184 
    System.Web.Mvc.Async.WrappedAsyncResult`1.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.WrappedAsyncResult`1.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.<MakeVoidDelegate>b__3(IAsyncResult ar) +47 
    System.Web.Mvc.Async.WrappedAsyncResult`1.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.WrappedAsyncResult`1.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() +9633216 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155 

在此先感謝。

+0

您能否將錯誤的完整文本添加到問題中?如果我們不確切知道這是什麼錯誤,那麼人們很難嘗試和幫助你! – JonK

+0

感謝任何反饋或建議。我用堆棧跟蹤編輯帖子。 –

回答

1

在調用我的Google-fu後,我發現this potentially applicable thread。對於後代(如果線程被徹底刪除或以其他方式無法訪問),這裏是相關位(爲簡便起見大大削減):

大家好我目前正在嘗試建立一把umbraco,我在 安裝數據庫的一步,但我得到以下錯誤

Exception Details: System.Data.SqlClient.SqlException: CREATE TABLE permission denied in database 'umbraco'. 

[SqlException (0x80131904): CREATE TABLE permission denied in database 'umbraco'.] 

另一個網站用戶回答如下:

你確定你有權限s在你的數據庫上創建表? 您可以在連接字符串中使用相同的帳戶登錄到您的數據庫中進行檢查。然後嘗試創建表,如果這不起作用修復該帳戶的 usercredentials。

注:如果你跟着教程,您connectionString應設置爲您的網頁下面的字母。配置文件:

<add name="MovieDBContext" 
    connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True" 
    providerName="System.Data.SqlClient" 
/> 

在此之後建議,OP回答:

感謝您的答覆。我發現了這個問題。右鍵點擊 數據庫 - >權限 - >選擇創建表格複選框。

你能按照這些步驟,看看它是否能解決你的問題?如果沒有,那麼恐怕你不得不等待有這方面專業知識的人前來幫助你,因爲我所能做的就是讓Google找到答案。

+0

謝謝你的迴應。不幸的是,在我工作的地方,我擁有的唯一權限就是創建存儲過程,即使在DEV中,也可以在我的模式下創建存儲過程。這應該是簡單的。並非所有公司都允許具有數據庫修改權限的用戶。 –

+0

@Web_developer_beginner沒有授予必要的權限,我真的無法看到這個工作。對我來說,似乎很奇怪,一家公司會讓某人在產品上工作,但不允許任何數據庫模式更改。只是似乎適得其反。無論如何,如果您沒有權限,您需要將它們授予您,或讓DBA創建表,以便開始使用它。 – JonK

+0

謝謝。抱歉。可能我之前並不清楚。該表已經在數據庫中。我只需要能夠訪問它,而不需要代碼來創建它。 –

相關問題