Pie*_*rto 0 c# database connection wpf entity-framework
喜欢标题。我该怎么做?
我尝试了一些东西,但是没有按我预期的那样工作。
我正在使用实体框架模型。我需要像参数一样传递连接字符串,因此,在另一个文件中,我编写了
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)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
启动应用程序时,我会以这种方式调用此构造函数,因此无论如何我都可以在应用程序中引用它:
public static MyEntities dbContext = new MyEntities(mdlImpostazioni.SetConnectionString());
Run Code Online (Sandbox Code Playgroud)
其中mdlImpostazioni.SetConnectionString()返回一个字符串(数据正确):
server=192.168.1.100\SVILUPPO;database=MyDB;uid=myName;pwd=111111;
Run Code Online (Sandbox Code Playgroud)
当我执行此代码时,似乎一切正常,但是当我尝试进行如下查询时:
var query = (from r in MainWindow.dbContext.TabTipoSistema select r);
Run Code Online (Sandbox Code Playgroud)
它从这里抛出异常:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException(); //exception here
}
Run Code Online (Sandbox Code Playgroud)
所以,这是一种糟糕的方法...正确的方法是?(仅使用代码C#,不使用xaml)
您的方法是正确的,但是您需要记住EF的连接字符串需要元数据等。因此,请使用EntityConnectionStringBuilder。例如:
// 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();
}
Run Code Online (Sandbox Code Playgroud)
编辑
您得到的异常是因为当您传递纯SQL连接字符串时,它假定您首先使用Code,因此它将调用OnModelCreation事件。如上所示,当您包括MetaData部分时,将告诉EF这是一个完整的EF连接字符串。