我正在 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 ,而且我似乎找不到它。 由于数据库未显示,我不确定这是否有效以及我的数据是否已实际存储。
有谁知道我做错了什么或者我是否遗漏了什么?
最佳答案
我好像已经解决了这个问题。
我做了以下事情:
- 在服务器资源管理器中,我右键单击“数据连接”,选择“添加连接...”并创建了一个 Microsoft SQL Server。我将服务器名称设置为
.\SQLEXPRESS
并将数据库名称设置为Library
。 - 然后我将以下内容添加到
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/