Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用connectionString访问现有表_C#_Sql Server_Asp.net Mvc_Entity_Connection String - Fatal编程技术网

C# 使用connectionString访问现有表

C# 使用connectionString访问现有表,c#,sql-server,asp.net-mvc,entity,connection-string,C#,Sql Server,Asp.net Mvc,Entity,Connection String,我正在编写一个web应用程序,它将访问/更新数据库中的几个现有表。目前,我正在尝试访问SQL express上数据库的本地版本以进行测试,但在搜索如何设置连接后,无法正确设置连接。My web.config文件当前的连接字符串设置如下: <add name="ContractDBContext" connectionString="Data Source=SBUWMPB01FHSM\SQLEXPRESS; Initial Catal

我正在编写一个web应用程序,它将访问/更新数据库中的几个现有表。目前,我正在尝试访问SQL express上数据库的本地版本以进行测试,但在搜索如何设置连接后,无法正确设置连接。My web.config文件当前的连接字符串设置如下:

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

这是为DB表建模的类的代码:

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; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Data.Entity;
使用System.ComponentModel.DataAnnotations;
使用System.ComponentModel.DataAnnotations.Schema;
命名空间ForeignExchange.Models
{
[表(“会计数据”)]
公共类合同
{
[关键][必选]
公共int tkrconstract{get;set;}
公共整数AsOfMonth{get;set;}
public int AsOfQuarter{get;set;}
public int ASOfYear{get;set;}
公共双currentmovchg{get;set;}
公共双LTDFMVChg{get;set;}
公共双累积CI{get;set;}
公共双累加derivasset{get;set;}
公共双累积pl{get;set;}
公共双累积fas5{get;set;}
公共双历史spotrate{get;set;}
公共双历史Hedgedrate{get;set;}
}
[表(“会计数据”)]
公共类ContractDBContext:DbContext
{
公共数据库集契约{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: 
返回视图(db.Contracts.ToList()) EntityCommandExecutionException 执行命令定义时出错。 用户代码未处理System.Data.EntityCommandExecutionException HResult=-2146232004 消息=执行命令定义时出错。有关详细信息,请参见内部异常。 Source=System.Data.Entity 堆栈跟踪: 位于System.Data.EntityClient.EntityCommandDefinition.ExecuteStorommands(EntityCommand EntityCommand,CommandBehavior) 在System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext上下文,ObjectParameterCollection parameterValues) 位于System.Data.Objects.ObjectQuery`1.GetResults(可为null`1 forMergeOption) 位于System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator() 位于System.Data.Entity.Internal.Linq.InternalQuery`1.GetEnumerator()处 位于System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator()处 位于System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator() 位于System.Collections.Generic.List`1..ctor(IEnumerable`1集合) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) 在C:\Users\stonejo\documents\visual studio 2010\Projects\ForeignExchange\ForeignExchange\Controllers\ContractsController.cs中的ForeignExchange.ContractsController.Index()处:第21行 在lambda_方法中(闭包、控制器基、对象[]) 位于System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,Object[]参数) 位于System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext ControllerContext,IDictionary`2参数) 位于System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext ControllerContext,ActionDescriptor ActionDescriptor,IDictionary`2参数) 在System.Web.Mvc.Async.AsyncControllerActionInvoker.c__DisplayClass42.b__41()中 在System.Web.Mvc.Async.AsyncResultRapper.c_uuu显示类8`1.b_uuu7(IAsyncResult) 位于System.Web.Mvc.Async.AsyncResultRapper.WrappedAsyncResult`1.End() 位于System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) 在System.Web.Mvc.Async.AsyncControllerActionInvoker.c_uuDisplayClass37.c_uuDisplayClass39.b_u33()中 在System.Web.Mvc.Async.AsyncControllerActionInvoker.c__DisplayClass4f.b__49()中 InnerException:System.Data.SqlClient.SqlException HResult=-2146232060 消息=无效的列名“AccumeratedFas5”。 Source=.Net SqlClient数据提供程序 错误代码=-2146232060 等级=16 行号=11 数字=207 Procedure=“” 服务器=SBUWMPB01FHSM\SQLEXPRESS 状态=1 堆栈跟踪: 位于System.Data.SqlClient.SqlConnection.OneError(SqlException异常,布尔断开连接,操作'1 wrapCloseInAction) 位于System.Data.SqlClient.SqlInternalConnection.OneError(SqlException异常,布尔断开连接,操作'1 wrapCloseInAction) 位于System.Data.SqlClient.TdsParser.ThroweException和Warning(TdsParserStateObject StateObjectStateObj、布尔调用方连接锁、布尔异步关闭) 位于System.Data.SqlClient.TdsParser.TryRun(RunBehavior RunBehavior、SqlCommand cmdHandler、SqlDataReader dataStream、BulkCopySimpleResultSet bulkCopyHandler、TdsParserStateObject stateObj、Boolean和dataReady) 位于System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()处 在System.Data.SqlClient.SqlDataReader.get_MetaData()处 位于System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds、RunBehavior、String ResetOptions String) 位于System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior、RunBehavior RunBehavior、Boolean returnStream、Boolean async、Int32超时、任务和任务、Boolean asyncWrite、SqlDataReader ds) 在System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior、RunBehavior RunBehavior、Boolean returnStream、String方法、T