喜欢标题。我该怎么办?
我尝试了一些东西,但它没有像我期待的那样起作用。
我正在使用实体框架模型。我需要传递我的连接字符串,如参数,因此,在另一个文件中,我写了
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)
{
}
}
}
当我启动应用程序时,我以这种方式调用此构造函数,所以我可以在应用程序中引用它:
public static MyEntities dbContext = new MyEntities(mdlImpostazioni.SetConnectionString());
其中mdlImpostazioni.SetConnectionString()返回一个字符串(数据正确):
server=192.168.1.100\SVILUPPO;database=MyDB;uid=myName;pwd=111111;
当我执行此代码时,似乎一切正常,但当我尝试进行如下查询时:
var query = (from r in MainWindow.dbContext.TabTipoSistema select r);
它从这里抛出异常:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException(); //exception here
}
所以,这是一个糟糕的方式......哪个是正确的? (仅使用代码C#,而不是使用xaml)
答案 0 :(得分:5)
您的方法是正确的,但您需要记住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();
}
修改强>
您获得的异常是因为当您传递纯SQL连接字符串时,它假定您首先使用Code,因此它调用OnModelCreation事件。当您包含如上所示的MetaData部分时,它告诉EF它是一个完整的EF连接字符串。
答案 1 :(得分:0)
我认为问题出在您指定的数据源上。您需要添加连接端口,例如,如果在端口1433上配置了SQL Server,请尝试:
server=192.168.1.100,1433\SVILUPPO;database=MyDB;uid=myName;pwd=111111;
有关连接字符串的更多详细信息,请参阅Here
此外,我不确定uid
和pwd
是否有效,请更好地尝试User ID
和Password
:
Server=192.168.1.100,1433\SVILUPPO;Database=MyDB;User ID=myName;Password=111111;
最后要注意区分大小写。