Reputation: 319
I'm trying to use the localdb with asp mvc I installed EF 6.0 from my visual studio 2013 (with nuget)
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);
}
}
}
}
When i run the "test", i have this message : "No connection string named 'BddContext' could be found in the application config file"
I think everything is good, but maybe something wrong ? Thank you for your help.
Upvotes: 2
Views: 2132
Reputation: 20033
The test is the one running the code. When it looks for a connectionstring, it looks in it's own project, not in your MVC project.
The fast and easy solution would be to add that connection string to your test project's app.config file as well.
Upvotes: 1
Reputation: 319
With your indications i found the solution :
Then visual studio generate a good app.config. Inside this file add the connectionstring.
Upvotes: 0
Reputation: 130
I am assuming you have a separate test project that contains your test class (DalTests). If you ensure the test project's app.config has the same BddContext connection string entry, the error should not occur, because the test project looks in its own config file, not that of the mvc project or any other libraries.
Upvotes: 0