当我尝试使用 EF 在 ASP.NET MVC5 中查询视图时,为啥会收到错误“数据库 'ELITEPROD' 中的 CREATE TABLE 权限被拒绝”?

Posted

技术标签:

【中文标题】当我尝试使用 EF 在 ASP.NET MVC5 中查询视图时,为啥会收到错误“数据库 \'ELITEPROD\' 中的 CREATE TABLE 权限被拒绝”?【英文标题】:Why am I getting the error "CREATE TABLE permission denied in database 'ELITEPROD'" when I'm trying to query a view in ASP.NET MVC5 using EF?当我尝试使用 EF 在 ASP.NET MVC5 中查询视图时,为什么会收到错误“数据库 'ELITEPROD' 中的 CREATE TABLE 权限被拒绝”? 【发布时间】:2021-09-03 07:57:16 【问题描述】:

我要做的只是查询一个视图,但我收到以下错误:

数据库“ELITEPROD”中的 CREATE TABLE 权限被拒绝

我正在使用实体框架编写 ASP.NET MVC5。这是我正在运行的代码:

string sql = @"SELECT ref.*, sts.[IMPORT_STATUS]
               FROM [ELITEPROD].[dbo].[SPEECH_PSYCH_WRITER] AS ref
               LEFT JOIN dbo.SPEECH_PSYCH_IMPORT_STATUS AS sts 
               ON sts.REQUEST_ID = ref.REFERRAL_ID
               WHERE REFERRAL_TYPE = @referType AND REFERRAL_CONTACT = @username 
               AND (sts.[IMPORT_STATUS] IS NULL OR sts.[IMPORT_STATUS] <> '');";

var referrals = eLite.Database.SqlQuery<Referral>(sql,
    new SqlParameter("@referType", referType),
    new SqlParameter("@username", username)
).ToList();

在 web 配置文件中,我配置了 2 个数据库。此查询发生在第二个查询上。不是DefaultConnection。数据库没有内存不足,我也没有(故意)尝试创建表。有什么想法吗?

这是完整的错误:

[SqlException (0x80131904): CREATE TABLE permission denied in database 'ELITEPROD'.]
System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +212
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +81
System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +630
System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +4233
System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite) +952
System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean& usedCache, Boolean asyncWrite, Boolean inRetry) +304
System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +282
System.Data.Entity.Infrastructure.Interception.<>c.<NonQuery>b__4_0(DbCommand t, DbCommandInterceptionContext`1 c) +12
System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch(TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed) +72
System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.NonQuery(DbCommand command, DbCommandInterceptionContext interceptionContext) +427
System.Data.Entity.Internal.InterceptableDbCommand.ExecuteNonQuery() +103
System.Data.Entity.Migrations.DbMigrator.ExecuteSql(MigrationStatement migrationStatement, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext) +109
System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection, DbTransaction transaction, DbInterceptionContext interceptionContext) +83
System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinTransaction(IEnumerable`1 migrationStatements, DbTransaction transaction, DbInterceptionContext interceptionContext) +65
System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsWithinNewTransaction(IEnumerable`1 migrationStatements, DbConnection connection, DbInterceptionContext interceptionContext) +155
System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection, DbInterceptionContext interceptionContext) +359
System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection) +509
System.Data.Entity.Migrations.<>c__DisplayClass61_0.<ExecuteStatements>b__0() +19
System.Data.Entity.SqlServer.<>c__DisplayClass2_0.<Execute>b__0() +10
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Func`1 operation) +189
System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation) +78
System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements, DbTransaction existingTransaction) +211
System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements) +7
System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, VersionedModel targetModel, IEnumerable`1 operations, IEnumerable`1 systemOperations, Boolean downgrading, Boolean auto) +938
System.Data.Entity.Migrations.DbMigrator.AutoMigrate(String migrationId, VersionedModel sourceModel, VersionedModel targetModel, Boolean downgrading) +589
System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) +424
System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration) +452
System.Data.Entity.Migrations.<>c__DisplayClass42_0.<Update>b__0() +13
System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase) +422
System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration) +78
System.Data.Entity.Internal.DatabaseCreator.CreateDatabase(InternalContext internalContext, Func`3 createMigrator, ObjectContext objectContext) +89
System.Data.Entity.Internal.InternalContext.CreateDatabase(ObjectContext objectContext, DatabaseExistenceState existenceState) +116
System.Data.Entity.Database.Create(DatabaseExistenceState existenceState) +218
System.Data.Entity.CreateDatabaseIfNotExists`1.InitializeDatabase(TContext context) +136
System.Data.Entity.Internal.<>c__DisplayClass66_0`1.<CreateInitializationAction>b__0() +76
System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +60
System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +357
System.Data.Entity.Internal.<>c.<InitializeDatabase>b__58_0(InternalContext c) +9
System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) +129
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) +196
System.Data.Entity.Internal.LazyInternalContext.InitializeDatabase() +86
System.Data.Entity.Internal.InternalContext.Initialize() +30
System.Data.Entity.Internal.LazyInternalContext.get_ObjectContext() +13
System.Data.Entity.Internal.InternalContext.ExecuteSqlQuery(String sql, Nullable`1 streaming, Object[] parameters) +109
System.Data.Entity.Internal.InternalContext.ExecuteSqlQueryAsIEnumerator(String sql, Nullable`1 streaming, Object[] parameters) +68
System.Data.Entity.Internal.InternalContext.ExecuteSqlQuery(Type elementType, String sql, Nullable`1 streaming, Object[] parameters) +181
System.Data.Entity.Internal.InternalSqlNonSetQuery.GetEnumerator() +41
System.Data.Entity.Infrastructure.DbRawSqlQuery`1.GetEnumerator() +30
System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +174
System.Linq.Enumerable.ToList(IEnumerable`1 source) +54
Report_Writers.Controllers.<Index>d__3.MoveNext() in C:\Users\craftjer\source\repos\report-writer-v2\Report Writers\Controllers\ReportsController.cs:59
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +102
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +64
System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +97
System.Web.Mvc.Async.<>c__DisplayClass8_0.<BeginInvokeAsynchronousActionMethod>b__1(IAsyncResult asyncResult) +17
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +32
System.Web.Mvc.Async.<>c__DisplayClass11_0.<InvokeActionMethodFilterAsynchronouslyRecursive>b__0() +58
System.Web.Mvc.Async.<>c__DisplayClass11_2.<InvokeActionMethodFilterAsynchronouslyRecursive>b__2() +228
System.Web.Mvc.Async.<>c__DisplayClass7_0.<BeginInvokeActionMethodWithFilters>b__1(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +34
System.Web.Mvc.Async.<>c__DisplayClass3_6.<BeginInvokeAction>b__4() +35
System.Web.Mvc.Async.<>c__DisplayClass3_1.<BeginInvokeAction>b__1(IAsyncResult asyncResult) +100
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +10
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +27
System.Web.Mvc.<>c.<BeginExecuteCore>b__152_1(IAsyncResult asyncResult, ExecuteCoreState innerState) +11
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +45
System.Web.Mvc.<>c.<BeginExecute>b__151_2(IAsyncResult asyncResult, Controller controller) +13
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +22
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +26
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10
System.Web.Mvc.<>c.<BeginProcessRequest>b__20_1(IAsyncResult asyncResult, ProcessRequestState innerState) +28
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +29
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +49
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +28
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +9
System.Web.CallHandlerExecutionStep.InvokeEndHandler(IAsyncResult ar) +152
System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +126

【问题讨论】:

这能回答你的问题吗? CREATE TABLE permission denied in database 'tempdb' Similar question 问题是数据库实例内存不足。 另一个可能的欺骗目标:Listing table results in “CREATE TABLE permission denied in database” ASP.NET - MVC4 【参考方案1】:

您的调用堆栈通过

System.Data.Entity.Database.Create(DatabaseExistenceState existenceState) +218

这意味着您的目标数据库不存在或与您的模型不是最新的,并且 EF 正在尝试更新架构。

也许您的连接字符串是错误的,但无论如何您都应该提前创建/迁移数据库架构,而不是在启动时。

【讨论】:

或者:完全禁用 EF 迁移并使用 SSDT 手动执行。我发现在任何真正复杂的项目中,或者当我需要使用新的 SQL Server 功能(仍在等待临时表、索引视图、表分区和EF 中的窗口功能支持...)【参考方案2】:

在 Models 文件夹中,我设置了 ELiteDbContext.cs 并且我有这个定义:

public DbSet<Referral> Referrals  get; set; 

由于这只是我正在查询的一个视图,并且永远不会写入它,我所要做的就是注释掉 DbSet 行,一切正常。

【讨论】:

尽管如此,当不优先工作时,turn off automatic migration 总是一个好主意。

以上是关于当我尝试使用 EF 在 ASP.NET MVC5 中查询视图时,为啥会收到错误“数据库 'ELITEPROD' 中的 CREATE TABLE 权限被拒绝”?的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET Mvc5+EF7

ASP.NET MVC5+EF6搭建三层实例

初探ASP.NET Mvc5+EF7的美妙旅程

初探ASP.NET Mvc5+EF7的美妙旅程

亲测Asp.net Mvc5 + EF6 code first 方式连接MySQL总结

基于 jQuery 的专业 ASP.NET WebForms/MVC 控件库!