0

I'm learning MVC and trying to follow some tutorials and this video http://channel9.msdn.com/Events/DevDays/DevDays-2011-Netherlands/Devdays002 In all tutorials a database is created automatically by EF(i suppose) in SQLEXPRESS but I want to use my SQLServer local instance. Changing default connection string:

<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
     providerName="System.Data.SqlClient" />

to:

<add name="ApplicationServices" connectionString="data source =(local); Initial Catalog =Test;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>

doesn't help and the default database still automatically created in SQLEXPRESS. Even totally commenting it out does nothing. I tried a few tutorials, but they all base on SQLExpress and mdf file, which I don't understand the purpose of. So... How can I make EF and my app talk to my local instance or any other instance ? I can provide more information on this, as I afraid I'm missing some important concepts. Thank you!

2
  • Does this "Test" db have the .mdf file restored to it? The top connection string attaches the DB to your local SQL instance at runtime, so I'm curious as to what steps you have taken to create a db on your machine to pull from rather than the file the project provides. Commented Oct 10, 2011 at 22:36
  • @Chuchelo what is the name of your local instance? Commented Oct 14, 2011 at 0:58

3 Answers 3

2

Make sure the connection string matches the name of your context.

If your context looks like...

public class MyContext : DbContext

Change your connection string name to MyContext

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

2 Comments

Tried that but getting an error: Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.People'. Source Error: Line 46: Line 47: db.People.Add(person); Line 48: db.SaveChanges(); Line 49: return RedirectToAction("Index"); Line 50: }
I added the comment above, forgot to mention your name.
2

The ApplicationServices connection string is only used for the Membership database. Your EF database will have it's own connection string, typically the name of the Context.

2 Comments

I think I tried what you meant, but had no luck, I posted the error as a comment for the answer from Chad. Do you think If I provide more code, you will be able to elaborate in more detail ? thanks!
@Chuchelo - Post your connection string you're using.
1

Most likely the problem is your machine.config having set up LocalSqlServer as .\SQLEXPRESS. You can change machine.config directly for the framework version you are using; or in the application web.config ConnectionStrings section use the following code:

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.