我如何以编程方式为entity frameworkCode-First设置连接string?
我试图写一些代码,允许我在SQLCE(本地在我的开发机器)和完整的SQL(在AppHarbor)之间切换。 使用SQL CE,连接string都是为我处理的,但是我必须自己构造SQL。 我的代码到目前为止,但是它给出了这个错误:
不支持的关键字:“元数据”
我一直在网上查找几个小时,但所有的解决scheme都涉及使用“ContextBuilder”类,我无法find(我通过NuGet包安装EF)。
这是当前的代码(通过WebActivator启动时运行):
public static void Start() { // Read the details from AppSettings. Locally, these will be empty. var databaseHost = ConfigurationManager.AppSettings["DatabaseHost"]; var databaseName = ConfigurationManager.AppSettings["DatabaseName"]; var databaseUsername = ConfigurationManager.AppSettings["DatabaseUsername"]; var databasePassword = ConfigurationManager.AppSettings["DatabasePassword"]; // Check whether we have actual SQL Server settings. if (!string.IsNullOrWhiteSpace(databaseHost) && !string.IsNullOrWhiteSpace(databaseName)) { // Set up connection string for a real live database :-O var connectionString = string.Format("metadata=res://*/DB.csdl|res://*/DB.ssdl|res://*/DB.msl;" + "provider=System.Data.SqlClient; provider connection string='Data Source={0};" + "Initial Catalog={1};User ID={2}; Password={3};MultipleActiveResultSets=True'", databaseHost, databaseName, databaseUsername, databasePassword); Database.DefaultConnectionFactory = new SqlConnectionFactory(connectionString); } else { // Set a custom database initializer for setting up dev database test data. Database.SetInitializer<BlogDataContext>(new BlogDataIntializer()); // Set the connection factory for SQL Compact Edition. Database.DefaultConnectionFactory = new SqlCeConnectionFactory("System.Data.SqlServerCe.4.0"); } }
你应该使用EntityConnectionStringBuilder类
string providerName = "System.Data.SqlClient"; string serverName = "."; string databaseName = "AdventureWorks"; // Initialize the connection string builder for the // underlying provider. SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder(); // Set the properties for the data source. sqlBuilder.DataSource = serverName; sqlBuilder.InitialCatalog = databaseName; sqlBuilder.IntegratedSecurity = true; // Build the SqlConnection connection string. string providerString = sqlBuilder.ToString(); // Initialize the EntityConnectionStringBuilder. EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(); //Set the provider name. entityBuilder.Provider = providerName; // Set the provider-specific connection string. entityBuilder.ProviderConnectionString = providerString; // Set the Metadata location. entityBuilder.Metadata = @"res://*/AdventureWorksModel.csdl| res://*/AdventureWorksModel.ssdl| res://*/AdventureWorksModel.msl"; Console.WriteLine(entityBuilder.ToString()); using (EntityConnection conn = new EntityConnection(entityBuilder.ToString())) { conn.Open(); Console.WriteLine("Just testing the connection."); conn.Close(); }
在entity framework代码中,首先使用SqlConnection。 你不能使用EntityConnectionStringBuilder,因为在代码中,没有元数据文件。
您也可以直接在您的上下文中进行设置。 您必须将mdf附加到您想要首先使用的SqlServer实例。 不完全优雅,但它为我工作
public void DoImportWork() { var ctx = new StatisticsContext(); << your DbContext ctx.Database.Connection.ConnectionString = @"Data Source=localhost\SQLEXP;AttachDbFilename=""C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXP\MSSQL\DATA\StatisticsData.mdf"";Integrated Security=True"; ctx.Database.Connection.Open(); }
像往常一样,当您将第一行添加到上下文时,EF会自动生成所有内容。