使用代码的方式给EntityFramework edmx 创建连接字符串

本文介绍如何在.NET中使用Entity Framework动态生成数据库连接字符串,并通过具体的C#代码示例展示了配置过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在构建上下文的时候动态生成连接字符串:

 

 

 /// <summary>
        /// 从配置生成连接
        /// </summary>
        private static readonly string _connStr = GetConnStringFromAppSetting();

        public context()
            : base(_connStr)//"name=context"
        {
         
        }
    
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        private static string GetConnStringFromAppSetting()
        {
            // Specify the provider name, server and database.
            string providerName ="System.Data.SqlClient"; 
            string providerString = global::System.Configuration.ConfigurationManager.AppSettings["SQLConnectionString"];//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://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl";

            var connString= entityBuilder.ToString();
            return connString;
        }

 

 

How to: Build an EntityConnection Connection String

This topic provides an example of how to build an EntityConnection.

To run the code in this example

  1. Add the AdventureWorks Sales Model to your project and configure your project to use the Entity Framework. For more information, see How to: Use the Entity Data Model Wizard.

  2. In the code page for your application, add the following using statements (Imports in Visual Basic):

    C#Copy
    using System;
    using System.Collections.Generic;
    using System.Collections;
    using System.Data.Common;
    using System.Data; using System.IO; using System.Data.SqlClient; using System.Data.EntityClient; using System.Data.Metadata.Edm; 

Example

The following example initializes the System.Data.SqlClient.SqlConnectionStringBuilder for the underlying provider, then initializes the System.Data.EntityClient.EntityConnectionStringBuilderobject and passes this object to the constructor of the EntityConnection.

C#Copy

// Specify the provider name, server and database.
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(); } 

See Also

How to: Use EntityConnection with an Object Context
EntityClient Provider for the Entity Framework

Note

转载于:https://www.cnblogs.com/micro-chen/p/8954193.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值