如何添加"供应商名称和QUOT;在连接字符串上下文文件? [英] How to add the "Provider Name" in Connection String to the Context file?

查看:173
本文介绍了如何添加"供应商名称和QUOT;在连接字符串上下文文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用实体框架5 code-先计算策略。这里是我的上下文文件:

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";
    }
}

}

在这里,我已经添加在构造函数中的连接字符串。但是,有什么办法可以添加提供程序名称来连接字符串?

推荐答案

是否有你为什么要在数据库方面的连接字符串硬codeD特殊的原因。通常它应存储在配置文件中。您可以指定配置文件中的供应商,并从上下文是指连接字符串。这将解决您的问题。

Is there a particular reason why you want to have the connection string hard coded in the db context. Normally it should be stored in the config file. You can specify the provider in the config file and refer the connection string from your context. That will solve your problem.

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

而在你的配置文件

And in your config file

<connectionStrings>
    <add name="MyDbContext" connectionString="data source=.\sqlexpress;initial catalog=YourDbName;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient"/>
  </connectionStrings>

这篇关于如何添加&QUOT;供应商名称和QUOT;在连接字符串上下文文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆