3

I am new to EF 5.0, i have purposely set the wrong database name in connection string in Web.Config. i.e. Initial Catalog = "Wrong Database Name";

Now I have created a boolean property, which detects whether the database exists or not. Like below - where context is the EFDbContext.

bool IsDatabaseExist = context.DataBase.Exists();

My question is, why this is always showing true, even if the DataBase name which I have provided is wrong ?

4
  • 1
    Are you sure the context didn't automatically create the database? Commented Oct 14, 2012 at 18:19
  • Hmm, need to check this, Can you detail a little more ? How to check this, or how to force that it doesn't create any database ? Commented Oct 14, 2012 at 18:23
  • Look in your SQL Server. Check EF's database initializer. Commented Oct 14, 2012 at 18:34
  • @SLaks Sorry but I am very new to using SQL Server and EF. Also I am using SQL Express. Commented Oct 14, 2012 at 18:39

1 Answer 1

1

If you did not provide connection string to the constructor of the DbContext derived class you are not using your connection string. The easiest way to do it is to create a parameterless constructor to in your DbContext derived class that calls DbContext constructor - something like this:

public class MyContext : DbContext
{
    public MyContext() : base("name=connectionStringName")
    { }
    ...
}

connectionString name is the name of the connection string from your config file.

If you want to check what connection string your application is using you can do the following:

((SqlConnection)myContext.Database.Connection).ConnectionString

myContext here is an instance of the DbContext derived class

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.