2014-07-02 22 views
1

我正在編寫一個Web應用程序,它將訪問/更新數據庫中的多個現有表。目前,我正在嘗試訪問SQL Express上的本地數據庫版本以用於測試目的,但在搜索後無法正確設置連接。我的web.config文件中目前有連接字符串設置如下:使用connectionString訪問現有表

<add name="ContractDBContext" 
    connectionString="Data Source=SBUWMPB01FHSM\SQLEXPRESS; 
         Initial Catalog=12-2013 Derivative TrackingSQL; 
         Integrated Security=True" 
    providerName="System.Data.SqlClient" /> 

這是類模型數據庫表的代碼:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Data.Entity; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 

namespace ForeignExchange.Models 
{ 
    [Table("AccountingData")] 
    public class Contract 
    { 
     [Key][Required] 
     public int TKRContractID { get; set; } 
     public int AsOfMonth { get; set; } 
     public int AsOfQuarter { get; set; } 
     public int ASOfYear { get; set; } 
     public double CurrentMoFMVChg { get; set; } 
     public double LTDFMVChg { get; set; } 
     public double AccumulatedOCI { get; set; } 
     public double AccumulatedDerivAsset { get; set; } 
     public double AccumulatedPL { get; set; } 
     public double AccumulatedFAS5 { get; set; } 
     public double HistoricalSpotRate { get; set; } 
     public double HistoricalHedgedRate { get; set; } 
    } 
    [Table("AccountingData")] 
    public class ContractDBContext : DbContext 
    { 
     public DbSet<Contract> Contracts { get; set; } 
    } 
} 

編寫代碼的兩段下達後上面,我使用「添加控制器」自動化對話框來生成CRUD .cshtml頁面和控制器,稱爲ContractsController。當我運行的代碼,並嘗試訪問應該顯示錶生成的索引頁,我得到的就行了以下錯誤

return View(db.Contracts.ToList()) 

EntityCommandExecutionExcception 
An error occurred while executing the command definition. 
System.Data.EntityCommandExecutionException was unhandled by user code 
    HResult=-2146232004 
    Message=An error occurred while executing the command definition. See the inner exception for details. 
    Source=System.Data.Entity 
    StackTrace: 
     at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) 
     at System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues) 
     at System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption) 
     at System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() 
     at System.Data.Entity.Internal.Linq.InternalQuery`1.GetEnumerator() 
     at System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() 
     at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() 
     at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) 
     at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) 
     at ForeignExchange.Controllers.ContractsController.Index() in C:\Users\stonejo\documents\visual studio 2010\Projects\ForeignExchange\ForeignExchange\Controllers\ContractsController.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`2 parameters) 
     at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) 
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41() 
     at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) 
     at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End() 
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) 
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() 
     at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() 
    InnerException: System.Data.SqlClient.SqlException 
     HResult=-2146232060 
     Message=Invalid column name 'AccumulatedFAS5'. 
     Source=.Net SqlClient Data Provider 
     ErrorCode=-2146232060 
     Class=16 
     LineNumber=11 
     Number=207 
     Procedure="" 
     Server=SBUWMPB01FHSM\SQLEXPRESS 
     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.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior) 
     InnerException: 

連接字符串和基於Web的應用程序是完全新的給我,讓我明白任何幫助某人可以爲此提供我。感謝您花時間閱讀我的問題。

+1

對於我來說,'Data Source =(localdb)\ v11.0'應該是'Data Source = ServerName \ DbName'。但我可能是錯誤的 –

+0

Fwiw有時當我在Visual Studio中遇到連接字符串問題時,我會轉到數據源,添加新的數據源並讓嚮導找出它。 –

+0

@ T.S。我試過用我的PCname \ SQLEXPRESS去,但遇到了同樣的錯誤。 – brostone51

回答

1

嘗試在Visual Studio中轉到數據源,添加新的數據源並讓嚮導找出它。如果需要,打開一個新的Visual Studio實例,創建一個新的解決方案/項目以獲得結果,然後將其複製/粘貼到原始解決方案中。