代码第一:连接字符串&数据库? [英] 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; }
}
我没有创建任何数据库,但我能够做所有的CRUD操作。
I didn't create any data base, but I was able to do all the CRUD operations.
现在我没有看到我的web.config中的连接字符串。我看不到数据库。我检查了App_Data目录以及Sql Server Express。我没有看到数据库创建的痕迹。
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 don't see any trace of the Database created.
然而,一切都很完美。
推荐答案
EF代码首先将使用与您的数据库上下文名称相同的连接字符串,因此可以这样定义:
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>
默认情况下不会创建数据库,直到你真正做某事,例如一旦您首次致电 EfDbContext.SaveChanges()
即可显示。
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.
如果您尚未定义自己的自定义连接字符串,它将被称为与您的数据库上下文( YourNamespace.EfDbContext
)相同,并且应显示在您的默认本地SQL实例。
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.
我的数据在哪里?
Where’s My Data?
DbContext按惯例为$ b创建一个数据库$ b localhost\SQLEXPRESS。数据库以您的派生上下文的完全限定的
名称命名,在我们的例子中是
CodeFirstSample.ProductContext。稍后我们将介绍如何更改此
。
这篇关于代码第一:连接字符串&数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!