Adding ADO.Net Entity Data Model using machine.config connection string

asp.net-mvc-5 c# entity-framework-6 machine.config web-config

Question

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

1
1
6/18/2014 11:47:53 AM

Accepted Answer

After several experiments on the MVC and EF Database First approach, I found that the problem was in the machine.config's connection string. Entity Framework doesn't like the standard connection string. Connection String must have the metadata Info about the DBContext resources and the provider name for EF. My standard ConnectionString in the machine.config was:

<add name="masterDataConnectoinString" connectionString="Data Source=Instance1;Initial Catalog=masterData;Integrated Security=True;"/>

Then I copied the ConnectionString from web.config to maching.config which was created by default when I added the Entity Data Model (edmx):

<add name="masterDataConnectionString" connectionString="metadata=res://*/Models.DBContext.csdl|res://*/Models.DBContext.ssdl|res://*/Models.DBContext.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=Instance1;initial catalog=masterData;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

And then specifying the connectionstring name to be used as I have shown in my question for MasterDataEntities, solves the problem.

Using machine.config's ConnectionString creates another issue. If you amend the database, e.g add another column into any table and would like to update the Entity Data Model (edmx) with the new change, you right click the edmx Diagram and then click Update Model from Database, which will not work. The wizard will ask you to specify the new connection, which is not right. It should offer you to Add/Refresh/Delete tables or views or procedures.

To solve this problem temporarily I kept the connectionString into my web.config but commented out. If I had to update the model from the database, uncomment the web.config connectionString and set back your (in my case) MasterDataEntities to use web.config:

public partial class MasterDataEntities : DbContext
{
    public MasterDataEntities()
       : base("name=masterDataConnectionString"))
    {
    }
}

Which updates the model. If anyone got a better solution please please let me know. Thanks

0
6/19/2014 10:41:00 AM

Popular Answer

You can simply define your connection string in machine.config. When you use the configuration manager it will look in the machine.config first for your appsettings and then your application config file.

Check this article on Configuration files

string innerConnectionString = ConfigurationManager.ConnectionStrings["Inner"].ConnectionString;

<add name="Inner" connectionString="Data Source=SomeServer;Initial Catalog=SomeCatalog;Persist Security Info=True;User ID=Entity;Password=SomePassword;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow