Question

Hi iam trying to configure my first application with EF but find no way to connect to DB

this is my context

public partial class FunzionamentoContext : DbContext
{
    public FunzionamentoContext()
        : base("FunzionamentoContext")
    {}

    static FunzionamentoContext()
    {
        Database.SetInitializer<FunzionamentoContext>(null);
    }

    public DbSet<Controllo> Controllo { get; set; }
}

this is connection string (present in app.config file of DAL project since i still don't have UI project):

<?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>
  <connectionStrings>
    <add name="FunzionamentoContext"
            providerName="System.Data.SqlClient"
            connectionString="Data Source=xyzxyz; Initial Catalog=dbName;
        Integrated Security=false;User ID=test;Password=test" />
  </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>

this is a simply method i make to test connection (only for search if connection is done, next i will rewrite it in a more beautyful way :D)

    [Test]
    public void TestTrue()
    {
        FunzionamentoContext fctx = new FunzionamentoContext();
        Controllo c = new Controllo();
        .id = 1
        fctx.Controllo.Add((Controllo)c);
        fctx.SaveChanges(); 
    }

When i try to connect i see this error:

FunzIA.UnitTest.DAL.TestFixture1.TestTrue: System.Data.Entity.Core.EntityException : The underlying provider failed on Open. ----> System.Data.SqlClient.SqlException : Cannot open database "FunzionamentoContext" requested by the login. The login failed. Login failed for user 'DOMAIN\U123456'.

where DOMAIN\U123456 is my windows account

Why EF use my windows account and not the one wrote in connection string?

Was it helpful?

Solution

I am assuming that you have your DAL in a separate project from the project you're actually running.

Your application will use the config file from the project you're trying to run, and not the one directly related to the DAL project.

By this, I mean, if you're only using the test project (assuming it's in a different project from the DAL) then you need the connection string in the app.config of your test project.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top