简体   繁体   中英

Entity Framework DBContext and dynamic ConnectionString

I'm wanting to make a connection dynamically using EF 4 in a Windows application. When trying busar a record, gives the error: Keyword not supported: 'provider'.

This error for code: var nota = db.NotasFiscais.Find(28406);

Codes are below

Main Application

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    NF.DataBase.Global.Conexao = new System.Data.EntityClient.EntityConnectionStringBuilder();
    NF.DataBase.Global.Conexao.Provider = "System.Data.SqlClient";
    NF.DataBase.Global.Conexao.ProviderConnectionString = "Data Source=source;Initial Catalog=base;User ID=sa; Password=pass;";

    Application.Run(new Form1());
}

DBContext

public class ERPContext : DbContext
{
    public DbSet<NotasFiscais> NotasFiscais { get; set; }
    public DbSet<NFEmissores> NFEmissores { get; set; }
    public DbSet<Cidades> Cidades { get; set; }

    public ERPContext()
        : base(NF.DataBase.Global.Conexao.ToString())
    {

        Database.SetInitializer<ERPContext>(null);
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<NotasFiscais>().ToTable("NotasFiscais");
        modelBuilder.Entity<NFEmissores>().ToTable("NFEmissores");
        modelBuilder.Entity<Cidades>().ToTable("Cidades");
    }
}

Font:

private void button1_Click(object sender, EventArgs e)
{
    var db = new ERPContext();
    var nota = db.NotasFiscais.Find(28406);
    ...
}

Change this:

  Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    NF.DataBase.Global.Conexao = new System.Data.EntityClient.EntityConnectionStringBuilder();
    NF.DataBase.Global.Conexao.Provider = "System.Data.SqlClient";
    NF.DataBase.Global.Conexao.ProviderConnectionString = "Data Source=source;Initial Catalog=base;User ID=user; Password=pass;";

for:

  Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    NF.DataBase.Global.Conexao = new System.Data.EntityClient.EntityConnectionStringBuilder();
// not provider
    NF.DataBase.Global.Conexao.ProviderConnectionString = "Data Source=source;Initial Catalog=base;User ID=user; Password=pass;";

Sources:

http://social.msdn.microsoft.com/Forums/vstudio/en-US/77ff9db1-c69d-4a14-b606-d208832b8756/keyword-not-supported-provider?forum=netfxbcl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM