4

I want to use Entity Framework without app.config file.

I want to define a string variable Connection String in my code and use that to connect to the database.

Please show me the way if it is possible.

Mahdi jokar
  • 1,267
  • 6
  • 30
  • 50

1 Answers1

8

You're not mentioning what approach you're using (database-first, model-first, code-first) - but basically, in the end, you need to define a string variable and assign it a valid EF connection string

string myConnectionString = "...(define a valid EF connection string here)......";

Example for database-first approach:

string myConnectionString = @"metadata=.\Model1.csdl|.\Model1.ssdl|.\Model1.msl;provider=System.Data.SqlClient;provider connection string="";data source=.;initial catalog=test;integrated security=True;multipleactiveresultsets=True;App=EntityFramework""";

and then use that to create your ObjectContext (database- and model-first) or DbContext (code-first)

using(ObjectContext ctx = new ObjectContext(myConnectionString))
{
    // do your EF magic here.....
}

But quite honestly - I think this is a really bad idea since this makes it impossible for you to move your application to another machine - no one else can install and run this, since the connection string is hard-coded into your C# code..... the whole point of having config files is so that you can change / adapt things like connection strings so that they are not tied to a single machine/location but can be adapted to the particular needs of a given user / customer....

VoteCoffee
  • 4,692
  • 1
  • 41
  • 44
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    +1 although... I get a trickle of regular work due to hard-coded database connection info, so maybe I should support doing this sort of thing! – Andrew Barber Jan 13 '12 at 16:02
  • where do you Handel myConnectionString.when i use this i get error – Mahdi jokar Jan 13 '12 at 16:47
  • 1
    While hard-coding the connection information may not be a good idea, there may be situations where it is not possible to use a config file. I've come across one: The class library I'm writing will be called by a Dynamics CRM application which cannot use a config file - the configuration settings must be passed into the constructor of the class. – Simon Elms Nov 12 '12 at 20:32
  • @SimonTewsi: true - but those are very rare edge cases - right? – marc_s Nov 12 '12 at 20:41
  • 1
    Rare maybe, but sometimes you need to pass the connection information in from another configuration source, so it's valuable to know how to do this. – Matthew Walton Nov 14 '12 at 10:45
  • 1
    We store our settings in the registry, and so we read from the registry and pass it into this constructor. – Tom Beech Dec 06 '13 at 21:51
  • I'm using your advice but am running into an issue. Please see http://stackoverflow.com/questions/23656542/entity-framework-6-10-and-database-first-with-hard-coded-connection-string if you get a chance. – VoteCoffee May 14 '14 at 13:48
  • For me, this is useful in testing. I can materialize the entity but the navigation properties fail when there's not a connection string configuration. Any idea why? – Keith Sep 20 '16 at 18:19
  • You migt also want to use a connection string builder such as SqlConnectionStringBuilder to get the connection string – Keith Sep 20 '16 at 18:20