views:

66

answers:

2

Hello, I'm using Entity Framework in my project. The Framework has created its own connection string, so my web.config connectionStrings section file looks following:

  <connectionStrings>
    <add name="ApplicationServices" connectionString="data source=TEST186;user id=vnkuser;pwd=vnkuser;initial catalog=VNK" providerName="System.Data.SqlClient" />    
    <add name="VNKEntities" connectionString="metadata=res://*/Models.CMSModel.csdl|res://*/Models.CMSModel.ssdl|res://*/Models.CMSModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;Data Source=TEST186;Initial Catalog=VNK;User ID=vnkuser;Password=vnkuser;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>

The first connection string called ApplicationServices is my original one. The second, called VNKEntities has been created while generating the model.

When I checked the generated *.edmx file, I found that this model is referencing its connection string, which is shown below:

    /// <summary>
    /// Initializes a new VNKEntities object using the connection string found in the 'VNKEntities' section of the application configuration file.
    /// </summary>
    public VNKEntities() : base("name=VNKEntities", "VNKEntities")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

My question is how can I get rid of the VNKEntities connection string, and leave only ApplicationServices, to which I will be referencing from my model ? I would like to have only one connection string to the database, because I'm using only one database (replacing the constructor parameter from name=VNKEntities to name=ApplicationServices is not working).

Regards

+1  A: 
  1. Manualy create DbConnection from ordinary connection string
  2. Manualy create MetadataWorkspace object.
  3. Create EntityConnection using this ctor.
  4. Pass entity connection to ObjectContext constructor.
gandjustas
I'd like to avoid manual creation of this object using previously created connection. The parameterless constructor is more convenient so I'd like to find solution for such case.
Jarek Waliszko
+1  A: 

Although you can create the connection in code, as @gandjustas points out (+1), you cannot get away from having a connection string or EntityConnection.

This is because it is not actually redundant. Yes, the database connection part is redundant, and @gandjustas showed you how to remove that redundancy. However, the entity framework connection string also contains information about your model, which is not found anywhere in the connection string you wish to keep. This model information has to come from somewhere. Were you to eliminate the entity framework's connection string and use the parameter list constructor on ObjectContext, you would have eliminated all references to the model.

Craig Stuntz
Thanks for the detailed explanation, which helped me to understand why I should choose solution given by @gandjustas for solving my problem.
Jarek Waliszko