Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# EntityFramework忽略connectionString中的登录名_C#_Entity Framework_Connection String - Fatal编程技术网

C# EntityFramework忽略connectionString中的登录名

C# EntityFramework忽略connectionString中的登录名,c#,entity-framework,connection-string,C#,Entity Framework,Connection String,您好,我正在尝试使用EF配置我的第一个应用程序,但找不到连接到DB的方法 这就是我的背景 public partial class FunzionamentoContext : DbContext { public FunzionamentoContext() : base("FunzionamentoContext") {} static FunzionamentoContext() { Database.SetInitiali

您好,我正在尝试使用EF配置我的第一个应用程序,但找不到连接到DB的方法

这就是我的背景

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

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

    public DbSet<Controllo> Controllo { get; set; }
}
当我尝试连接时,我看到以下错误:

FunzIA.UnitTest.DAL.TestFixture1.TestTrue: System.Data.Entity.Core.EntityException:基础提供程序在打开时失败。 ---->System.Data.SqlClient.SqlException:无法打开登录请求的数据库“FunzionamentoContext”。登录失败。 用户“域\U123456”登录失败

其中域\U123456是我的windows帐户


为什么EF使用我的windows帐户而不是在连接字符串中写入的帐户?

我假设您的DAL位于与实际运行的项目不同的项目中

您的应用程序将使用您试图运行的项目中的配置文件,而不是与DAL项目直接相关的配置文件


我的意思是,如果您只使用测试项目(假设它位于与DAL不同的项目中),那么您需要测试项目的app.config中的连接字符串。

我假设也是这样。如果未找到任何连接字符串,EF将尝试使用Windows身份验证连接到本地SQL Server。是的,但由于他提供了连接字符串名称,因此在
base()
FunzionamentoContext
)中,EF将仅使用该字符串,而不会进一步替换“FunzionamentoContext”在连接字符串名称及其完整命名空间中。是的,在测试项目中添加连接字符串将使连接正常工作。我对Andrew的建议很感兴趣,但不需要更多的解释:所以可以说test project的正确配置文件在哪里?是的,如果DAL项目名称空间是
MyProject
,您应该能够将
MyProject.FunzionamentoContext
作为名称(如果内存可用…)-我倾向于避开这一点,因为我喜欢我正在运行的设置DB的项目-有一天,您可能会有多个项目使用您的DAL项目,并且需要不同的DB
<?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>
    [Test]
    public void TestTrue()
    {
        FunzionamentoContext fctx = new FunzionamentoContext();
        Controllo c = new Controllo();
        .id = 1
        fctx.Controllo.Add((Controllo)c);
        fctx.SaveChanges(); 
    }