I am Using Entity Framework 5 Code-first approch. Here is my Context file :
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";
}
}
}
Here I've added the connection string in the constructor. But is there any way to add the "Provider Name" to the connection string?
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>