Code first: Where's the connection string & the Database?
I'm testing how code first works. Here's how I defined the Context
public class EfDbContext: Context
{
public DbSet<Client> Clients { get; set; }
}
I didn't create any data base, but I was able to do all the CRUD operations.
Now I don't see the connection string in my web.config. I don't see either the Database. I've checked the App_Data Directory and also the Sql Server Express. I 开发者_StackOverflowdon't see any trace of the Database created.
Yet, everything is perfectly.
EF code-first will use a connection string that has the same name as your DB context - so you could define it like this:
<connectionString>
<add name="EfDbContext"
connectionString="server=YourServer;database=YourChoice;Integrated Security=SSPI;" />
</connectionString>
The database won't be created by default, until you actually do something, e.g. it should show up as soon as you make a call to EfDbContext.SaveChanges()
for the first time.
It will be called the same as your DB context (YourNamespace.EfDbContext
) if you haven't defined your own, custom connection string, and it should show up in your default local SQL instance.
See from the ADO.NET EF 4.1 Code First Walkthrough:
Where’s My Data?
DbContext by convention created a database for you on localhost\SQLEXPRESS. The database is named after the fully qualified name of your derived context, in our case that is “CodeFirstSample.ProductContext”. We’ll look at ways to change this later in the walkthrough.
精彩评论