Search code examples
c#entity-frameworkxunit

System.InvalidOperationException : No connection string named 'db_modelContainer' could be found in the application config file


I wanted to test a method that creates an account.

public users CreateAccount(string _username, double _weight, string _password)
{
    using (var db = new db_modelContainer())
    {
        Daily_summary summary = new Daily_summary { weight = _weight};
        Users_dishes_gallery user_dishes = new Users_dishes_gallery { };

        var x = db.usersSet;
        foreach (var i in x)
        {
            if (_username == i.name)
            {

                throw new CreateAccountFailException("Username is already occupied!");
            }
        }

        users newuser = new users { name = _username, weight = _weight, password = _password};

        
        db.usersSet.Add(newuser);
        db.SaveChanges();
        return newuser;
    }
}

I have another xUnit project where I wanted to write those tests. I prepared my first file:

[Fact]
//[AutoRollback]
public void CreateAccount_GivenNotOccupiedUsername_CreateSucceed()
{

    string expectedLogin = "test";
    string expectedPassword = "test";
    double expectedWeight = 30;

    Users user = new Users();
    users createduser = user.CreateAccount(expectedLogin, expectedWeight,expectedPassword);
    
    // Assert
    Assert.Equal(expectedLogin, createduser.name);
    Assert.Equal(expectedPassword, createduser.password);
    Assert.Equal(expectedWeight, createduser.weight);

}

But I'm still getting that error.

error from vs

System.InvalidOperationException : No connection string named 'db_modelContainer' could be found in the application config file.

I tried many ways. Main project with edmx inside is my start project. I also added a link to App. config from my main project so my test project has access to the connection string but it didn't help me.

link to app.config

Below is my App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
  </startup>
  <connectionStrings>
    <add name="db_modelContainer" connectionString="metadata=res://*/Database.db_model.csdl|res://*/Database.db_model.ssdl|res://*/Database.db_model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost\SQLEXPRESS;initial catalog=Dietaverse_database;integrated security=True;multipleactiveresultsets=True;application name=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

Solution

  • Where is your App.Config file? If it's not hosted in the same project as your Unit Test then it probably isn't being loaded into the program. Try making a copy of your App.Config file and adding it to your Unit Test project and setting the property to "Copy Always"