C# No connection string named 'TravelsDbEntities' could be found in the application config file

System.InvalidOperationException   HResult=0x80131509   Message=No connection string named 'TravelsDbEntities' 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.Internal.Linq.InternalSet`1.ActOnSet(Action action, EntityState newState, Object entity, String methodName)    在 System.Data.Entity.Internal.Linq.InternalSet`1.Add(Object entity)    在 System.Data.Entity.DbSet`1.Add(TEntity entity)    在 DAL.CityDal.saveOneCityList(List`1 cityList) 在 C:\Users\liuzhao\source\repos\Travel\DAL\CityDal.cs 中: 第 17 行    在 BLLs.CrawlerManagerBll.TestCity() 在 C:\Users\liuzhao\source\repos\Travel\BLLs\CrawlerManagerBll.cs 中: 第 25 行    在 TravelCrawler.Program.Main(String[] args) 在 C:\Users\liuzhao\source\repos\Travel\TravelCrawler\Program.cs 中: 第 14 行

1、找到项目中用EF生成的实体模型所在类库

C# No connection string named 'TravelsDbEntities' could be found in the application config file

2、点击Models类库里的App.config

C# No connection string named 'TravelsDbEntities' could be found in the application config file

copy:

<connectionStrings>

    <add name="TravelsDbEntities" connectionString="metadata=res://*/DataModels.csdl|res://*/DataModels.ssdl|res://*/DataModels.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=LIUZHAO110;initial catalog=TravelsDb;persist security info=True;user id=sa;password=123;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

  </connectionStrings>

然后分别粘贴到DAL、BLL、以及Web层中的App.config里就ok了

C# No connection string named 'TravelsDbEntities' could be found in the application config file

 

具体原因就是在根据数据生成实体模型时会在app.config生成对应的链接数据库的配置,但是其他类库要通过DBContext操作数据库时他们类库中app.config配置文件里确没有相应的链接数据库配置,所以报链接不到数据库的错误