15

我正在使用 Entity Framework 5 代码优先方法。这是我的上下文文件:

using IMS.Domain.Inventory;
using IMS.Domain.Security;
using IMS.Domain.StoredProcedures;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Objects;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IMS.Domain.DBContext
{
    public class IMSDBContext : DbContext
    {
        public DbSet<ModuleAccounting> ModuleAccountings { get; set; }
        public DbSet<ModuleInfo> ModuleInfos { get; set; }
        public DbSet<ModuleType> ModuleTypes { get; set; }
        public DbSet<UserAccounting> UserAccountings { get; set; }
        public DbSet<UserGroup> UserGroups { get; set; }
        public DbSet<UserInfo> UserInfos { get; set; }


    //
    // set a connection string

    public IMSDBContext()  // Constructor of the Context
    {
        this.Database.Connection.ConnectionString =
            "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=IMSDB;Data Source=.\\SQLExpress";
    }
}

}

在这里,我在构造函数中添加了连接字符串。但是有没有办法将“提供者名称”添加到连接字符串中?

4

2 回答 2

11

是否有特殊原因要在 db 上下文中硬编码连接字符串。通常它应该存储在配置文件中。您可以在配置文件中指定提供程序并从您的上下文中引用连接字符串。这将解决你的问题。

 public MyDbContext()
        : base("Name=MyDbContext")
    {
    }

在你的配置文件中

<connectionStrings>
    <add name="MyDbContext" connectionString="data source=.\sqlexpress;initial catalog=YourDbName;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient"/>
  </connectionStrings>
于 2013-08-25T13:50:33.360 回答
10

Yes: You can prepare a DbConnection type that can be passed to DbContext which was built by the underlying provider and has the connection string built properly.

So to achieve this connection string in CODE... see below

<connectionStrings>
    <add name="MyCTX" connectionString="Data Source=localhost;Initial Catalog=MYDB ;Integrated Security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
  </connectionStrings>

recall, that DBContext has an overloaded constructor

 public DbContext(DbConnection existingConnection, bool contextOwnsConnection)

So you just need the Dbconnection built by the underlying factory provider. See

 public interface IDbConnectionFactory

which is implmented by these 3 types:

System.Data.Entity.Infrastructure.SqlCeConnectionFactory System.Data.Entity.Infrastructure.LocalDbConnectionFactory System.Data.Entity.Infrastructure.SqlConnectionFactory

So here is an example using SQLConnectionFactory. That returns a DBConnection. Which can be passed to DBContext. You can repeat/change or make variable at your programming leisure. For the other 2 providers.

 public DbConnection GetSqlConn4DbName(string dataSource, string dbName) {
        var sqlConnStringBuilder = new SqlConnectionStringBuilder();
        sqlConnStringBuilder.DataSource = String.IsNullOrEmpty(dataSource) ? DefaultDataSource : dataSource;
        sqlConnStringBuilder.IntegratedSecurity = true;
        sqlConnStringBuilder.MultipleActiveResultSets = true;
        // NOW MY PROVIDER FACTORY OF CHOICE, switch providers here 
        var sqlConnFact = new SqlConnectionFactory(sqlConnStringBuilder.ConnectionString);
        var sqlConn = sqlConnFact.CreateConnection(dbName);
        return sqlConn;
    }
于 2013-08-25T14:25:19.043 回答