EntityFramework忽略connectionString中的登录名 [英] EntityFramework ignore login name in connectionString

查看:118
本文介绍了EntityFramework忽略connectionString中的登录名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用EF配置我的第一个应用程序,但没有办法连接到DB



这是我的上下文

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

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

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

这是连接字符串(存在于DAL项目的app.config文件中我还没有UI项目):

 <?xml version =1.0encoding =utf-8 ?> 
< configuration>
< configSections>
<! - 有关实体框架配置的更多信息,请访问http://go.microsoft.com/fwlink/?LinkID=237468 - >
< section name =entityFrameworktype =System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection,EntityFramework,Version = 6.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089requirePermission =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.SqlClienttype =System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer/>
< / providers>
< / entityFramework>
< / configuration>

这是一个简单的方法,我测试连接(仅当搜索连接完成后,下一个将以更美丽的方式重写它:D)

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

当我尝试连接我看到这个错误:



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



其中DOMAIN\U123456是我的Windows帐户


$ b $为什么EF使用我的Windows帐户,而不是连接字符串中写的那个?

解决方案

我假设你有您的DAL与您实际运行的项目的单独项目。



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



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


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?

解决方案

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.

这篇关于EntityFramework忽略connectionString中的登录名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆