C# 没有名为'的连接字符串;BddContext';可以在应用程序配置文件中找到
我正在尝试使用带有asp mvc的localdb 我从visual studio 2013安装了EF 6.0(使用nuget) 首先,在我的项目中,我添加了一个“ADO.NET实体数据模型”:C# 没有名为'的连接字符串;BddContext';可以在应用程序配置文件中找到,c#,asp.net,entity-framework,dbcontext,C#,Asp.net,Entity Framework,Dbcontext,我正在尝试使用带有asp mvc的localdb 我从visual studio 2013安装了EF 6.0(使用nuget) 首先,在我的项目中,我添加了一个“ADO.NET实体数据模型”: 使用OCR\u.Models; 使用System.Data.Entity; 使用System.Data.Entity.ModelConfiguration.Conventions; 餐厅名称空间 { 使用制度; 使用System.Data.Entity; 使用System.Linq; 公共类BddCont
使用OCR\u.Models;
使用System.Data.Entity;
使用System.Data.Entity.ModelConfiguration.Conventions;
餐厅名称空间
{
使用制度;
使用System.Data.Entity;
使用System.Linq;
公共类BddContext:DbContext
{
公共BddContext()
:base(“name=BddContext”)
{
}
公共DbSet子代{get;set;}
公共DbSet Restos{get;set;}
}
然后我的界面:
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间OCR_.Models
{
接口:IDisposable
{
void CreerRestaurant(字符串名称、字符串电话);
列出obtientouslessrestaurants();
}
}
我的朋友:
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
名称空间OCR_.Models
{
公共类:IDal
{
私人bdd上下文bdd;
公营部门()
{
bdd=新的BddContext();
}
公共列表无障碍餐厅()
{
返回bdd.Restos.ToList();
}
公共空间处置()
{
bdd.Dispose();
}
公共无效CreerRestaurant(字符串名称、字符串电话)
{
Resto Resto=newresto{Id=1,Nom=Nom,Telephone=Telephone};
bdd.Restos.Add(resto);
}
}
}
在my Web.config中:
这是我的测试课:
使用系统;
使用Microsoft.VisualStudio.TestTools.UnitTesting;
使用System.Data.Entity;
使用OCR_.Models;
使用System.Collections.Generic;
使用System.Linq;
名称空间OCR_.Tests
{
[测试类]
公开课测验
{
[测试方法]
公共无效CreerRestaurant_avecu nouveaurestaturant_obtientouslesstaurants renvoitbienlerestaurant()
{
使用(Dal=new Dal())
{
dal.CreerRestaurant(“La bonne fourchette”,“01 02 03 04 05”);
List restos=dal.obtientouslessrestaurants();
Assert.IsNotNull(restos);
Assert.AreEqual(1,restos.Count);
AreEqual(“La bonne fourchette”,restos[0].Nom);
Assert.AreEqual(“01 02 03 04 05”,restos[0].电话);
}
}
}
}
当我运行“测试”时,我收到以下消息:
“在应用程序配置文件中找不到名为'BddContext'的连接字符串”
我认为一切都很好,但可能是出了什么问题?
谢谢您的帮助。我假设您有一个包含测试类(DalTests)的单独测试项目。如果您确保测试项目的app.config具有相同的BddContext连接字符串条目,则不应发生错误,因为测试项目在其自己的配置文件中查找,而不是在mvc项目或任何其他库中查找。测试是运行代码的。在查找连接字符串时,它会在其自己的配置文件中查找项目,而不是在MVC项目中
快速简便的解决方案是将连接字符串添加到测试项目的app.config文件中。根据您的指示,我找到了解决方案:
- 右clic testproject并添加名为“应用程序配置文件”的项
- 在testprject中:我还必须安装实体框架
using OCR_Restaurant.Models;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
namespace OCR_Restaurant
{
using System;
using System.Data.Entity;
using System.Linq;
public class BddContext : DbContext
{
public BddContext()
: base("name=BddContext")
{
}
public DbSet<Sondage> Sondages { get; set; }
public DbSet<Resto> Restos { get; set; }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace OCR_Restaurant.Models
{
interface IDal: IDisposable
{
void CreerRestaurant(string nom, string telephone);
List<Resto> ObtientTousLesRestaurants();
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace OCR_Restaurant.Models
{
public class Dal:IDal
{
private BddContext bdd;
public Dal()
{
bdd = new BddContext();
}
public List<Resto> ObtientTousLesRestaurants()
{
return bdd.Restos.ToList();
}
public void Dispose()
{
bdd.Dispose();
}
public void CreerRestaurant(string nom, string telephone)
{
Resto resto = new Resto { Id = 1, Nom = nom, Telephone = telephone };
bdd.Restos.Add(resto);
}
}
}
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<connectionStrings>
<add name="BddContext" connectionString="data source=(LocalDb)\v11.0;initial catalog=OCR_Restaurant.BddContext;integrated security=TRUE;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
</connectionStrings>
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data.Entity;
using OCR_Restaurant.Models;
using System.Collections.Generic;
using System.Linq;
namespace OCR_Restaurant.Tests
{
[TestClass]
public class DalTests
{
[TestMethod]
public void CreerRestaurant_AvecUnNouveauRestaurant_ObtientTousLesRestaurantsRenvoitBienLeRestaurant()
{
using (Dal dal = new Dal())
{
dal.CreerRestaurant("La bonne fourchette", "01 02 03 04 05");
List<Resto> restos = dal.ObtientTousLesRestaurants();
Assert.IsNotNull(restos);
Assert.AreEqual(1, restos.Count);
Assert.AreEqual("La bonne fourchette", restos[0].Nom);
Assert.AreEqual("01 02 03 04 05", restos[0].Telephone);
}
}
}
}