0

I have a code first database in a WCF project. I use it by calling

using (var db = new DbContext())

I have another console application. I've added a reference to the WCF project. When I use

using (var db = new DbContext())

it creates a new database in the database next to the original database.

Database Database.DbContext

How do I get them to use the same database?

2
  • Okay so the problem was that there was no connection string specified so it created one based on Namespace.DbContext. How can I share connection strings, though, to avoid any errors should the connection string change? Commented May 26, 2014 at 18:39
  • The connection strings typically don't change much IMO. You should have a good mechanism for managing them for different environments though. The "Ferrari" version of that is to do it through some sort of connection string replacement during automated deployments. Commented May 26, 2014 at 18:41

3 Answers 3

1

I believe you need to edit the app.config files of both applications and make sure the connection strings point to the same database.

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

4 Comments

I was piggy backing off the connection string in the other application.
what do you mean by "piggy backing"?
I didn't specify the connection string at all and it automatically started using the connection string from the other project's settings.
I don't think that Entity Framework is using the connection string from the other project's settings. Unless the second project references the first, there's no way the second should know of the first's existence. Without seeing the app.config files, I can't say for sure.
1

Make sure the connection strings are the same across both applications

1 Comment

I was piggy backing off the connection string in the other application.
1

Create separate class for dbContext and provide your db name in constructor method

public class dbContext : DbContext
{
public dbContext() :base("YourDbName") { }

}

After that you can create instance as below:

dbContext db = new dbContext();

Hope it helps!

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.