Connection String without web.config?

Discussion of open issues, suggestions and bugs regarding Entity Framework support in ADO.NET Data providers
Post Reply
ColorsFade
Posts: 3
Joined: Wed 09 Feb 2011 21:24

Connection String without web.config?

Post by ColorsFade » Wed 09 Feb 2011 21:33

I could not find this in the documentation, but I am wondering how I can setup the ObjectContext without using the web.config to store the connection string.

I noticed while looking at the generated code for the base ObjectContext that there are three constructors. One takes a EntityConnection as a parameter and another version takes a connection string.

Is there a way to do this?

Thanks.

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

Post by AndreyR » Thu 10 Feb 2011 13:57

You can use the ObjectContext constructor that takes a connection string, like in the following example:

Code: Select all

Entities1 db = new Entities1("metadata=res://*/Model2.csdl|res://*/Model2.ssdl|res://*/Model2.msl;provider=Devart.Data.Oracle;provider connection string=\"User Id=andreyr;Password=*;Server=*;Persist Security Info=True\"")
As an alternative, you can use the constructor that takes an EntityConnection parameter, initializing the EntityConnection using the EntityConnectionStringBuilder class.

ColorsFade
Posts: 3
Joined: Wed 09 Feb 2011 21:24

Post by ColorsFade » Thu 10 Feb 2011 14:25

Thanks Andrey, but that doesn't help much. See, I'm using a DomainContext and not newing up the entities like you are.

Code: Select all

 [EnableClientAccess()]
  public class ProjectStaffingService : LinqToEntitiesDomainService
I have no idea how to handle this and we cannot allow the connection string to be in the app.config.

ColorsFade
Posts: 3
Joined: Wed 09 Feb 2011 21:24

Post by ColorsFade » Thu 10 Feb 2011 15:08

I figured it out. You have to overwrite the CreateObjectContext() method in the service.

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

Post by AndreyR » Thu 10 Feb 2011 16:19

You are correct. Thank you for sharing the solution.

Post Reply