实体框架DBContext和动态ConnectionString
本文关键字:动态 ConnectionString DBContext 框架 实体 | 更新日期: 2023-09-27 18:01:21
我想在Windows应用程序中动态地使用EF 4进行连接。当尝试busar记录时,给出错误:关键字不支持:'provider'。
代码:var nota = db.NotasFiscais.Find(28406);
代码如下
主应用程序
[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");
}
}
字体:private void button1_Click(object sender, EventArgs e)
{
var db = new ERPContext();
var nota = db.NotasFiscais.Find(28406);
...
}
修改如下:
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;";
为: 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;";
来源:http://social.msdn.microsoft.com/Forums/vstudio/en-US/77ff9db1-c69d-4a14-b606-d208832b8756/keyword-not-supported-provider?forum=netfxbcl