First code: where is the connection string and database?

I am testing how the code works. This is how I defined the context

public class EfDbContext: Context { public DbSet<Client> Clients { get; set; } } 

I did not create any database, but I was able to perform all CRUD operations.

Now I do not see the connection string in my web.config. I do not see any database. I checked the App_Data and Sql Server Express directory. I do not see any traces of the created database.

But everything is fine.

+4
source share
1 answer

EF code-first will use a connection string with the same name as your DB context, so you can define it like this:

 <connectionString> <add name="EfDbContext" connectionString="server=YourServer;database=YourChoice;Integrated Security=SSPI;" /> </connectionString> 

The database will not be created by default until you actually do nothing, for example. it should appear as soon as you make the call to EfDbContext.SaveChanges() for the first time.

It will be called the same as your DB context ( YourNamespace.EfDbContext ) if you did not specify your own custom connection string and it should appear in your local SQL instance by default.

See the ADO.NET EF 4.1 section . First walkthrough :

Where is my data?

By convention, DbContext created a local \ SQLEXPRESS database for you. The database is named after the fully qualified name of your derived context, in our case, which is "CodeFirstSample.ProductContext". See how to change this later in the walkthrough.

+4
source

All Articles