c# - 数据库未显示在 Visual Studio 中

标签 c# database visual-studio-2010

我正在 Visual Studio 中使用 C# 开发应用程序。这是我的第一个 C# 应用程序,它将使用本地数据库,因此我不确定这是如何完成的。 我一直在关注this article来自 codeguru。

我已经声明了我的实体,所有实体目前都只是继承自:

public class Reference
{
    /// <summary>
    /// ID of the reference.
    /// </summary>
    public int Id { get; set;  }

    /// <summary>
    /// Reference to the element in theTVDB.
    /// </summary>
    public int TheTVDBId { get; set; }

    /// <summary>
    /// Whether or not the reference has been marked as watched.
    /// </summary>
    public bool IsWatched { get; set; }
}

我还声明了我的 DbContext 如下:

public class Library : DbContext
{
    /// <summary>
    /// Constructor using the base constructor.
    /// This constructor names the database "Library".
    /// </summary>
    public Library() : base("Library")
    {
    }

    /// <summary>
    /// Set of TVSeriesReferences stored in the database.
    /// </summary>
    public DbSet<TVSeriesReference> TVSeriesReferences { get; set; }

    /// <summary>
    /// Set of SeasonReferences stored in the database.
    /// </summary>
    public DbSet<SeasonReference> SeasonReferences { get; set; }

    /// <summary>
    /// Set of EpisodeReferences stored in the database.
    /// </summary>
    public DbSet<EpisodeReference> EpisodeReferences { get; set; }
}

我正在尝试将实体存储在数据库中,执行以下操作:

Library db = new Library();

TVSeriesReference reference1 = new TVSeriesReference();
reference1.TheTVDBId = 1234;
reference1.IsWatched = true;
db.TVSeriesReferences.Add(reference1);

TVSeriesReference reference2 = new TVSeriesReference();
reference2.TheTVDBId = 8000;
db.TVSeriesReferences.Add(reference2);

int i = db.SaveChanges();

所有这些似乎都有效。至少,我没有收到任何错误,而且每次运行时 i 都是 2。

问题是数据库(名为“Library”)没有出现在任何地方。实际上,我什至没有那个“对象资源管理器” View ,而且我似乎找不到它。 由于数据库未显示,我不确定这是否有效以及我的数据是否已实际存储。

有谁知道我做错了什么或者我是否遗漏了什么?

最佳答案

我好像已经解决了这个问题。

我做了以下事情:

  1. 在服务器资源管理器中,我右键单击“数据连接”,选择“添加连接...”并创建了一个 Microsoft SQL Server。我将服务器名称设置为 .\SQLEXPRESS 并将数据库名称设置为 Library
  2. 然后我将以下内容添加到 app.config: 它现在似乎起作用了。
<connectionStrings>
    <add name="Library"
         providerName="System.Data.SqlClient"
         connectionString="Data Source=.\SQLEXPRESS;Database=Library;Trusted_Connection=true;" />
</connectionStrings>

关于c# - 数据库未显示在 Visual Studio 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12960213/

相关文章:

c# - 在多个文件中搜索多个字符串

c# - 围绕 utc 日期的问题 - TimeZoneInfo.ConvertTimeToUtc 导致日期更改

c# - 如何确定我的 EF 6 数据库初始化程序是否会运行?

sql - 使用 sql server 检查字谜

.net - 我可以使用 WinForms VB.NET 应用程序访问中央服务器上的数据库吗?

css - VS 2010 - 我需要扩展来编辑 LESS 样式表

c# - 应用程序.Current.Shutdown();在未引用的程序集中定义

c# - 为什么在从泛型继承时必须使用父类型限定才能使用嵌套类型?

c# - Protobuf.NET 并列出对象 : can't add System. 字符串的已知子类型?

mysql - 代表单个食品订单中的多个菜肴的最佳数据结构/数据库架构是什么?