gpt4 book ai didi

entity-framework - 在 SQL Server Management Studio 中看不到 EF 数据库

转载 作者:行者123 更新时间:2023-12-01 03:54:47 26 4
gpt4 key购买 nike

我已经使用 EF 代码优先创建了一个数据库,就像那里 http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspx .但是当我通过 add() 向 db 输入数据时然后调用savechanges()我在 SQL Server 数据库文件夹中没有看到新数据库,也没有异常(exception)。这样对吗?我在哪里可以找到我的数据库以及如何将它放在数据库文件夹中?

我使用以下代码:

     public class Name
{
public long NameId { get; set; }
public string FullName { get; set; }
}

public class InfoContext : DbContext
{
public DbSet<Name> Names { get; set; }
}

然后我称之为:
      var db = new InfoContext();
var Names = new Name
{
NameId = 1,
FullName = "test"
};
db.Names.Add(Name);

db.SaveChanges();
var test = db.Names.Find(1);//there I get correct value

我在 web.config 中有 connectionString,如下所示:
     <connectionStrings>
<add name="InfoName" providerName="System.Data.SqlClient"
connectionString="Server = .\MYINSTANCE; Initial Catalog=mydbname;" />
</connectionStrings>

最佳答案

根据您的意见,您需要修改 web.config项目根目录中的文件(不是 Views 文件夹中的文件。您可以在其中添加如下部分:

<connectionStrings>
<add name="EFDbContext" connectionString="Data Source = .; Initial Catalog = ITSDB; Integrated Security = true" providerName="System.Data.SqlClient"/>
</connectionStrings>

元素的 name 属性是您的 Data 模型的 DbContext 名称,因此如果您的类定义为:
public class SomeContext : DbContext
{
...
}

那么你的配置应该是:
<connectionStrings>
<add name="SomeContext" connectionString="Data Source = .; Initial Catalog = ITSDB; Integrated Security = true" providerName="System.Data.SqlClient"/>
</connectionStrings>

至于连接字符串,它取决于您的数据库。

关于entity-framework - 在 SQL Server Management Studio 中看不到 EF 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18279458/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com