Entity Framework Model Not Using web.config connection strng

Discussion of open issues, suggestions and bugs regarding Entity Developer - ORM modeling and code generation tool
Post Reply
johnwest80
Posts: 27
Joined: Thu 09 Jun 2011 22:32

Entity Framework Model Not Using web.config connection strng

Post by johnwest80 » Thu 09 Jun 2011 22:36

I don't know how to set up a connection string in my web.config that my generated model will use. Can someone provide an example? I can't find any mention of this in any documentation or in the forums.

Thanks.

AndreyR
Devart Team
Posts: 2919
Joined: Mon 07 Jul 2008 13:16

Post by AndreyR » Fri 10 Jun 2011 08:32

I recommend you to use the ObjectContext constructor taking the string parameter.
However, there is a default connection string that should already be written to your web.config. You can just modify it.

johnwest80
Posts: 27
Joined: Thu 09 Jun 2011 22:32

Post by johnwest80 » Sat 11 Jun 2011 00:44

I already had a model, and I'm opening it outside of Visual Studio. So I need to create the web.config entry manually, which is why I'm asking. Can you show me how the web.config should be modified? Thanks.

AndreyR
Devart Team
Posts: 2919
Joined: Mon 07 Jul 2008 13:16

Post by AndreyR » Tue 14 Jun 2011 10:29

See the following MSDN article - How to: Define the Connection String.
For more details about the EDM connection string, refer to the Connection Strings article.

johnwest80
Posts: 27
Joined: Thu 09 Jun 2011 22:32

Post by johnwest80 » Tue 14 Jun 2011 14:27

Sorry for my frustration, but I know how to define connection strings. I co-authored a few books on web dev and .net with Wrox. I'm an expert googler. I need a little more depth in an answer. I need a concrete example, and an explanation, about where your model looks for a connection string. I saw in the generated code that a section is mentioned. I've tried every possible way of having a connection string in the web.config that I could think of, I've tried to look at your code for models, and I just can't get it to read the web.config. It will only use the internal, hard-coded connection string.

Here's more detail... in my web.config, I have the following:





However, even when I set the password to one that is obviously incorrect or remove it entirely, data access still works.

Here's the generated code the mentions a section.

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


So, I need 1) a better understanding of what name of a connection string it's looking for, 2) where exactly in the web.config it should be (appsettings, , some custom , and 3) any help on troubleshooting why it's not picking up the connection string I have defined and was already being used successfully for the model prior to my opening and saving it with Entity Developer.

Hopefully that makes my problem a little clearer. Thanks.

johnwest80
Posts: 27
Joined: Thu 09 Jun 2011 22:32

Post by johnwest80 » Tue 14 Jun 2011 16:39

Man, I sound like a bigheaded jerk in my post above. Just trying to let you know where my skill level is, but no excuses. I apologize.

AndreyR
Devart Team
Posts: 2919
Joined: Mon 07 Jul 2008 13:16

Post by AndreyR » Wed 15 Jun 2011 09:56

Thank you for the clarification.
If you are creating a model in the stand-alone application of Entity Developer, you should follow these steps:
1. Open your Web Application in Visual Studio.
2. Add the existing model (.edml, .edps, Diagram1.view files - the Designer.cs will be automatically generated).
3. Rebuild the solution (This is necessary to generate the embedded metadata resources).
4. Create an entry in the web.config section as in the following example: 5. Call the constructor that I have mentioned as shown in the following code snippet:

Code: Select all

var ctx = new TestModel.TestEntities("name=TestEntities");

Post Reply