1. <legend id='l0QqE'><style id='l0QqE'><dir id='l0QqE'><q id='l0QqE'></q></dir></style></legend>
  2. <tfoot id='l0QqE'></tfoot>
      <i id='l0QqE'><tr id='l0QqE'><dt id='l0QqE'><q id='l0QqE'><span id='l0QqE'><b id='l0QqE'><form id='l0QqE'><ins id='l0QqE'></ins><ul id='l0QqE'></ul><sub id='l0QqE'></sub></form><legend id='l0QqE'></legend><bdo id='l0QqE'><pre id='l0QqE'><center id='l0QqE'></center></pre></bdo></b><th id='l0QqE'></th></span></q></dt></tr></i><div id='l0QqE'><tfoot id='l0QqE'></tfoot><dl id='l0QqE'><fieldset id='l0QqE'></fieldset></dl></div>
        <bdo id='l0QqE'></bdo><ul id='l0QqE'></ul>

      <small id='l0QqE'></small><noframes id='l0QqE'>

      如何以编程方式设置实体框架代码优先的连接字符串?

      How do I programmatically set the connection string for Entity-Framework Code-First?(如何以编程方式设置实体框架代码优先的连接字符串?)
          <tbody id='n65UE'></tbody>

        • <legend id='n65UE'><style id='n65UE'><dir id='n65UE'><q id='n65UE'></q></dir></style></legend>
            <i id='n65UE'><tr id='n65UE'><dt id='n65UE'><q id='n65UE'><span id='n65UE'><b id='n65UE'><form id='n65UE'><ins id='n65UE'></ins><ul id='n65UE'></ul><sub id='n65UE'></sub></form><legend id='n65UE'></legend><bdo id='n65UE'><pre id='n65UE'><center id='n65UE'></center></pre></bdo></b><th id='n65UE'></th></span></q></dt></tr></i><div id='n65UE'><tfoot id='n65UE'></tfoot><dl id='n65UE'><fieldset id='n65UE'></fieldset></dl></div>

              <tfoot id='n65UE'></tfoot>

              • <bdo id='n65UE'></bdo><ul id='n65UE'></ul>

                <small id='n65UE'></small><noframes id='n65UE'>

                本文介绍了如何以编程方式设置实体框架代码优先的连接字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在尝试编写一些代码,允许我在 SQLCE(在我的开发机器上本地)和完整 SQL(在 AppHarbor 上)之间切换.使用SQL CE,连接字符串都为我处理,但我必须为SQL自己构建它.到目前为止,我的代码如下,但是它给出了这个错误:

                I'm trying to write some code that allows me to switch between SQLCE (locally on my dev machine) and full SQL (on AppHarbor). With SQL CE, the connection string is all handled for me, but I have to construct it myself for SQL. My code so far is below, however it gives this error:

                不支持关键字:'元数据'

                Keyword not supported: 'metadata'

                我已经在网上找了几个小时,但所有的解决方案都涉及使用我找不到的ContextBuilder"类(我已经通过 NuGet 包安装了 EF).

                I've been looking online for hours, but all the solutions involve using a "ContextBuilder" class which I can't find (I've installed EF via the NuGet package).

                这是当前代码(通过 WebActivator 在启动时运行):

                Here's the current code (running at startup via 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();
                }
                

                这篇关于如何以编程方式设置实体框架代码优先的连接字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Building a comma separated list?(建立一个逗号分隔的列表?)
                Errors in SQL Server while importing CSV file despite varchar(MAX) being used for each column(尽管每列都使用了 varchar(MAX),但在导入 CSV 文件时 SQL Server 中出现错误)
                How can I import an Excel file into SQL Server?(如何将 Excel 文件导入 SQL Server?)
                Export table to file with column headers (column names) using the bcp utility and SQL Server 2008(使用 bcp 实用程序和 SQL Server 2008 将表导出到带有列标题(列名称)的文件)
                Concat field value to string in SQL Server(将字段值连接到 SQL Server 中的字符串)
                SQL Server Bulk insert of CSV file with inconsistent quotes(SQL Server 批量插入带有不一致引号的 CSV 文件)
                  <tbody id='t1peb'></tbody>
                        <bdo id='t1peb'></bdo><ul id='t1peb'></ul>
                      • <small id='t1peb'></small><noframes id='t1peb'>

                        <tfoot id='t1peb'></tfoot>
                      • <legend id='t1peb'><style id='t1peb'><dir id='t1peb'><q id='t1peb'></q></dir></style></legend>

                          <i id='t1peb'><tr id='t1peb'><dt id='t1peb'><q id='t1peb'><span id='t1peb'><b id='t1peb'><form id='t1peb'><ins id='t1peb'></ins><ul id='t1peb'></ul><sub id='t1peb'></sub></form><legend id='t1peb'></legend><bdo id='t1peb'><pre id='t1peb'><center id='t1peb'></center></pre></bdo></b><th id='t1peb'></th></span></q></dt></tr></i><div id='t1peb'><tfoot id='t1peb'></tfoot><dl id='t1peb'><fieldset id='t1peb'></fieldset></dl></div>