2015-02-05 60 views
0

我已經將數據庫視圖添加到SQL Server 2012.之後,我更新了我的EntityFramework(v6)模型,並構建並驗證了它。從ASP.Net WebAPI(v2)項目中刪除並添加了引用我的EntityFramework庫,以確保我引用了這個最新的更新。控制器具有到上下文和EF模型類的所有正確鏈接。當WebAPI控制器執行時EntityFramework 6模型的更新沒有被反映

public IEnumerable<RTV_DedicationComments> Get(int EntryNum) 
    { 
     //var query = from c in context.RTV_GetAllDedicationsNames orderby c.DedicationName where c.DedicationName.StartsWith(Name) select c; 
     var query = context.RTV_DedicationComments 
          .Where(d => d.EntryNumber == EntryNum) 
          .OrderBy(d => d.EntryNumber) 
          .Select(d => d); 

     var results = query.ToList(); 

     return results; 
    } 

在運行時上設置InnerException返回以下錯誤:: {「無效的列名稱PlatEntryNumber'\ r \ n無法使用視圖或函數的引用我從這個GET方法所做的更新沒有錯誤由於綁定錯誤的「dbo.RTV_DedicationComments」「}當執行查詢:

var results = query.ToList(); 

‘PlatEntryNumber’是從數據庫和之前的更新EF模型UT它已被刪除,它不會顯示作爲控制器中的dbcontext的字段。

我打開SQL Server事件探查器,看看該查詢執行那裏,它做:

exec sp_executesql N'SELECT 
[Project1].[EntryNumber] AS [EntryNumber], 
[Project1].[CommentDate] AS [CommentDate], 
[Project1].[Comment] AS [Comment], 
[Project1].[CommentBy] AS [CommentBy] 
FROM (SELECT 
    [Extent1].[EntryNumber] AS [EntryNumber], 
    [Extent1].[CommentDate] AS [CommentDate], 
    [Extent1].[Comment] AS [Comment], 
    [Extent1].[CommentBy] AS [CommentBy] 
    FROM (SELECT 
[RTV_DedicationComments].[EntryNumber] AS [EntryNumber], 
[RTV_DedicationComments].[CommentDate] AS [CommentDate], 
[RTV_DedicationComments].[Comment] AS [Comment], 
[RTV_DedicationComments].[CommentBy] AS [CommentBy] 
FROM [dbo].[RTV_DedicationComments] AS [RTV_DedicationComments]) AS [Extent1] 
    WHERE ([Extent1].[EntryNumber] = @p__linq__0) AND (@p__linq__0 IS NOT NULL) 
) AS [Project1] 
ORDER BY [Project1].[EntryNumber] ASC',N'@p__linq__0 int',@p__linq__0=5943651 

還有什麼我可以檢查刪除的WebAPI和EF之間的這些數據庫對象引用的殘餘?

這裏是整個堆棧跟蹤:

System.Data.Entity.Core.EntityCommandExecutionException was unhandled by user code 
    HResult=-2146232004 
    Message=An error occurred while executing the command definition. See the inner exception for details. 
    Source=EntityFramework 
    StackTrace: 
     at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) 
     at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues) 
     at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassb.<GetResults>b__a() 
     at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess) 
     at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClassb.<GetResults>b__9() 
     at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation) 
     at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) 
     at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0() 
     at System.Lazy`1.CreateValue() 
     at System.Lazy`1.LazyInitValue() 
     at System.Lazy`1.get_Value() 
     at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext() 
     at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) 
     at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) 
     at RecWebAPI.Controllers.RTV_GetDedicationCommentsController.Get(Int32 PlatEntryNum) in e:\Development Test\RecTaxWebSLN3\RecWebAPI3\Controllers\RTV_GetDedicationCommentsController.cs:line 43 
     at lambda_method(Closure , Object , Object[]) 
     at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters) 
     at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) 
     at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken) 
    InnerException: System.Data.SqlClient.SqlException 
     HResult=-2146232060 
     Message=Invalid column name 'PlatEntryNumber'. 
Could not use view or function 'dbo.RTV_DedicationComments' because of binding errors. 
     Source=.Net SqlClient Data Provider 
     ErrorCode=-2146232060 
     Class=16 
     LineNumber=3 
     Number=207 
     Procedure=RTV_DedicationComments 
     Server=mysqlserver 
     State=1 
     StackTrace: 
      at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) 
      at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) 
      at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) 
      at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) 
      at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() 
      at System.Data.SqlClient.SqlDataReader.get_MetaData() 
      at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString) 
      at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds) 
      at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite) 
      at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) 
      at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method) 
      at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior) 
      at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) 
      at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<>c__DisplayClassb.<Reader>b__8() 
      at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TInterceptionContext,TResult](Func`1 operation, TInterceptionContext interceptionContext, Action`1 executing, Action`1 executed) 
      at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext) 
      at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior) 
      at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) 
      at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) 
     InnerException: 
+0

你有沒有試過返回List而不是IEnumerable? – lucuma 2015-02-05 22:45:18

+0

我正在使用IEnumerable是所有其他獲取方法,並且這一個在fieldname更改之前成功運行,所以我沒有嘗試過。 – user2589090 2015-02-05 22:50:31

+0

返回列表導致了相同的錯誤。 – user2589090 2015-02-05 22:52:33

回答

0

的問題是與我的WebAPI的web.config文件。有一個指向舊數據庫的標籤。這對我來說有些困惑,因爲我認爲EF處理了到數據庫的所有連接,因此不需要WebAPI擁有數據庫信息。它正在工作。