问题描述
喜欢标题。我该怎么办?
我尝试了一些,但是不能像我预期的那样工作。
我使用一个Entity Framework模型。我需要传递我的连接字符串像参数,所以,在另一个文件,我写了
命名空间MyNamespace.Model
{
using System;
using System.Data.Entity;
使用System.Data.Entity.Infrastructure;
public partial class MyEntities:DbContext
{
public MyEntities(string nameOrConnectionString):base(nameOrConnectionString)
{
} $ b}
}
当我启动应用程序时,我以这种方式调用此构造函数,所以我可以参考这个从任何在应用程序:
public static MyEntities dbContext = new MyEntities(mdlImpostazioni.SetConnectionString );
其中mdlImpostazioni.SetConnectionString()返回一个字符串(数据正确):
server = 192.168.1.100\SVILUPPO; database = MyDB; uid = myName; pwd = 111111;
当我执行这个代码,似乎一切OK,但当我尝试进行查询例如:
var query =(从MainWindow.dbContext.TabTipoSistema select r中的r);
它会抛出此处的异常:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException(); // exception here
}
所以,这是一个坏的方法...正确的那一个? (只使用代码C#,而不是来自xaml)
你的方法是正确的,但你需要记住EF的连接字符串需要元数据等等。所以使用EntityConnectionStringBuilder。例如:
// app.config连接字符串中的模型名称(任何模型名称 - Model1?)
private static string GetConnectionString(string model,YourSettings settings)
{
//使用可配置的设置构建提供程序连接字符串
var providerSB = new SqlConnectionStringBuilder
{
/ /你也可以传递sql连接字符串作为参数,而不是设置
InitialCatalog = settings.InitialCatalog,
DataSource = settings.DataSource,
UserID = settings.User,
= settings.Password
};
var efConnection = new EntityConnectionStringBuilder();
//或没有提供程序连接的基于配置文件的连接string
// var efConnection = new EntityConnectionStringBuilder(@metadata = res://*/model1.csdl | res:// * / model1。 ssdl | res://*/model1.msl; provider = System.Data.SqlClient;);
efConnection.Provider =System.Data.SqlClient;
efConnection.ProviderConnectionString = providerSB.ConnectionString;
//基于您是否选择向构造函数提供app.config连接字符串
efConnection.Metadata = string.Format(res:// * / Model。{0} .csdl | res :// * / Model。{0} .ssdl | res:// * / Model。{0} .msl,model); ;
return efConnection.ToString();
}
//或者只传递连接字符串
private static string GetConnectionString(string model,string providerConnectionString)
{
var efConnection = new EntityConnectionStringBuilder();
//或没有提供程序连接的基于配置文件的连接string
// var efConnection = new EntityConnectionStringBuilder(@metadata = res://*/model1.csdl | res:// * / model1。 ssdl | res://*/model1.msl; provider = System.Data.SqlClient;);
efConnection.Provider =System.Data.SqlClient;
efConnection.ProviderConnectionString = providerConnectionString;
//基于您是否选择向构造函数提供app.config连接字符串
efConnection.Metadata = string.Format(res:// * / Model。{0} .csdl | res :// * / Model。{0} .ssdl | res:// * / Model。{0} .msl,model);
//确保res:// * / ...与配置文件中已有的内容相匹配。
return efConnection.ToString();
}
EDIT
你得到的异常是因为当你传递一个纯SQL连接字符串时,它假定你先使用Code,所以它调用OnModelCreation事件。当你包含如上所示的MetaData部分时,它告诉EF它是一个完整的EF连接字符串。
Like the title. How can I do it?
I tried something, but it doesn't work like I was expecting.
I'm using an Entity Framework model. I need to pass my connection string like parameter, so, in another file, I've written
namespace MyNamespace.Model
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class MyEntities: DbContext
{
public MyEntities(string nameOrConnectionString) : base(nameOrConnectionString)
{
}
}
}
When I startup the app, I call this constructor in this way, so I can refer to this from anyway in the app:
public static MyEntities dbContext = new MyEntities(mdlImpostazioni.SetConnectionString());
where mdlImpostazioni.SetConnectionString() returns a string (the data are correct):
server=192.168.1.100\SVILUPPO;database=MyDB;uid=myName;pwd=111111;
When I execute this code, it seems to be all ok, but when I try to make a query like:
var query = (from r in MainWindow.dbContext.TabTipoSistema select r);
it throws an exception from here:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException(); //exception here
}
So, this is a bad way... which is the right one? (using only code C#, not from xaml)
Your approach is correct, but you need to remember the connection string for EF requires the metadata and so on. So use the EntityConnectionStringBuilder. For example:
// the model name in the app.config connection string (any model name - Model1?)
private static string GetConnectionString(string model, YourSettings settings)
{
// Build the provider connection string with configurable settings
var providerSB = new SqlConnectionStringBuilder
{
// You can also pass the sql connection string as a parameter instead of settings
InitialCatalog = settings.InitialCatalog,
DataSource = settings.DataSource,
UserID = settings.User,
Password = settings.Password
};
var efConnection = new EntityConnectionStringBuilder();
// or the config file based connection without provider connection string
// var efConnection = new EntityConnectionStringBuilder(@"metadata=res://*/model1.csdl|res://*/model1.ssdl|res://*/model1.msl;provider=System.Data.SqlClient;");
efConnection.Provider = "System.Data.SqlClient";
efConnection.ProviderConnectionString = providerSB.ConnectionString;
// based on whether you choose to supply the app.config connection string to the constructor
efConnection.Metadata = string.Format("res://*/Model.{0}.csdl|res://*/Model.{0}.ssdl|res://*/Model.{0}.msl", model); ;
return efConnection.ToString();
}
// Or just pass the connection string
private static string GetConnectionString(string model, string providerConnectionString)
{
var efConnection = new EntityConnectionStringBuilder();
// or the config file based connection without provider connection string
// var efConnection = new EntityConnectionStringBuilder(@"metadata=res://*/model1.csdl|res://*/model1.ssdl|res://*/model1.msl;provider=System.Data.SqlClient;");
efConnection.Provider = "System.Data.SqlClient";
efConnection.ProviderConnectionString = providerConnectionString;
// based on whether you choose to supply the app.config connection string to the constructor
efConnection.Metadata = string.Format("res://*/Model.{0}.csdl|res://*/Model.{0}.ssdl|res://*/Model.{0}.msl", model);
// Make sure the "res://*/..." matches what's already in your config file.
return efConnection.ToString();
}
EDIT
The exception you get is because when you pass a pure SQL connection string, it assumes you are working with Code first, so it calls the OnModelCreation event. When you include the MetaData section as shown above, that tells EF it's a complete EF connection string.
这篇关于如何:实体框架的动态连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!