I am at the beginner level of learning MVC5 using EF6. Basically I am trying to use a connection string defined in machine.config rather than using web.config whilst creating the ADO.NET Entity Data Model. The reason why I am using machine.config for connection string is; we have different SQL Cluster servers and application servers. On local machine we use local SQL Server instance but on beta/Live SQL server we use different instance names. The credentials are different too. so on each application/web server we have defined the ConnectionString in machine.config for the same database name but with different credentials.
Is there a way I can use ConnectionString specified in machine.config whilst using the wizard to create ADO.Net Entity Data Model or can I give reference of machine.config ConnectionString to my app web.config or is there any other way to solve this problem?
I am using Database First technique using MVC5. I have tried the following after creating edmx by using local DB and then tried to change the connection string in my context class:
public partial class MasterDataEntities : DbContext
{
public MasterDataEntities()
: base(string.Format("{0}", getConnectionString()))
{
}
public static string getConnectionString()
{
Configuration config = ConfigurationManager.OpenMachineConfiguration();
return config.ConnectionStrings.ConnectionStrings["masterdata"].ConnectionString;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
}
Error: OnModelCreating Additional information: Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.
Kind Regards