simonbs
simonbs

Reputation: 8042

Database not showing up in Visual Studio

I'm developing an application using C# in Visual Studio. This is my first C# application which will use a local database and I am therefore unsure exactly how this is done. I have been following this article from 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:

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.

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?

Upvotes: 2

Views: 14748

Answers (1)

simonbs
simonbs

Reputation: 8042

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. I set the server name to .\SQLEXPRESS and the database name to Library.
  2. I then added the following to app.config: It seems to work now.
<connectionStrings>
    <add name="Library"
         providerName="System.Data.SqlClient"
         connectionString="Data Source=.\SQLEXPRESS;Database=Library;Trusted_Connection=true;" />
</connectionStrings>

Upvotes: 1

Related Questions