简体   繁体   English

数据库未显示在Visual Studio中

[英]Database not showing up in Visual Studio

I'm developing an application using C# in Visual Studio. 我在Visual Studio中使用C#开发应用程序。 This is my first C# application which will use a local database and I am therefore unsure exactly how this is done. 这是我的第一个C#应用程序,它将使用本地数据库,因此我不确定该如何完成。 I have been following this article from codeguru. 我一直在关注codeguru的这篇文章

I have declared my entities, all of which are currently just inheriting from this: 我已经声明了我的实体,所有这些实体目前都只是继承自此:

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; }
}

I have also declared my DbContext as the following: 我还声明了我的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; }
}

I am trying to store entities in the database doing the following: 我正在尝试执行以下操作将实体存储在数据库中:

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();

All of this seems to work. 所有这些似乎都有效。 At least, I get no errors and i is 2 on every run. 至少,我没有得到任何错误, i是2在每次运行。

The problem is that the database (which is named "Library") does not show up anywhere. 问题是数据库(名为“库”)没有显示在任何地方。 Actually, I don't even have that "Object Explorer" view and I can't seem to find it. 实际上,我什至没有“对象资源管理器”视图,而且似乎也找不到。 As the database doesn't show up, I am unsure whether or not this is working and if my data is actually stored. 由于没有显示数据库,因此我不确定这是否有效以及我的数据是否已实际存储。

Does anyone know what I am doing wrong or if I am missing something? 有人知道我在做什么错还是我错过了什么?

I seem to have solved this problem. 我似乎已经解决了这个问题。

I did the following: 我做了以下事情:

  1. In the Server Explorer, I right clicked the Data Connections, chose "Add Connection..." and created a Microsoft SQL Server. 在服务器资源管理器中,我右键单击“数据连接”,选择“添加连接...”并创建一个Microsoft SQL Server。 I set the server name to .\\SQLEXPRESS and the database name to Library . 我将服务器名称设置为.\\SQLEXPRESS ,数据库名称设置为Library
  2. I then added the following to app.config : It seems to work now. 然后,我在app.config添加了以下内容:现在看来可以工作了。
<connectionStrings>
    <add name="Library"
         providerName="System.Data.SqlClient"
         connectionString="Data Source=.\SQLEXPRESS;Database=Library;Trusted_Connection=true;" />
</connectionStrings>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM