C# No connection string named 'DataModelContainer' could be found in the application config file.
System.InvalidOperationException HResult=0x80131509 Message=No connection string named 'DataModelContainer' could be found in the application config file. Source=EntityFramework StackTrace: 在 System.Data.Entity.Internal.LazyInternalConnection.get_ConnectionHasModel() 在 System.Data.Entity.Internal.LazyInternalContext.InitializeContext() 在 System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) 在 System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() 在 System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() 在 System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() 在 DAL.ScenicDal.GetScenicArea(String cityName) 在 C:\Users\liuzhao\source\repos\Travel\DAL\ScenicDal.cs 中: 第 25 行 在 BLLs.ScenicAreaBll.GetScenicAreas(String name) 在 C:\Users\liuzhao\source\repos\Travel\BLLs\ScenicAreaBll.cs 中: 第 15 行 在 Travel.Controllers.TravelController.GetScenicArea(String city) 在 C:\Users\liuzhao\source\repos\Travel\Travel\Controllers\TravelController.cs 中: 第 26 行 在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass6_1.<GetExecutor>b__3(Object instance, Object[] methodParameters) 在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments) 在 System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)
由于我更改了模型实体,导致重新生成了sql server的连接配置
<connectionStrings>
<add name="DataModelContainer" connectionString="metadata=res://*/DataModel.csdl|res://*/DataModel.ssdl|res://*/DataModel.msl;provider=System.Data.SqlClient;provider connection string="data source=LIUZHAO110;initial catalog=TravelsDb;user id=sa;password=123;multipleactiveresultsets=True;application name=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
然而在其他项目的web.config下的配置不会更新,比如我的api接口项目中
<connectionStrings><add name="TravelsDbEntities" connectionString="metadata=res://*/Models.DataModel.csdl|res://*/Models.DataModel.ssdl|res://*/Models.DataModel.msl;provider=System.Data.SqlClient;provider connection string="data source=LIUZHAO110;initial catalog=TravelsDb;user id=sa;password=123;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /></connectionStrings>
这里命名还是之前的命名,所以导致这个问题,所以更改name后,其他所有的用到上下文的地方都得手动的更改过来,就是说将模型所在项目中的上面内容copy替换就行了。