Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8710099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T04:32:13+00:00 2026-06-13T04:32:13+00:00

I am trying to create my first database in a Visual Studio C# project

  • 0

I am trying to create my first database in a Visual Studio C# project and add an entity to this database. I have not yet managed to do so. When trying, I will get a DbUpdateException when calling SaveChanges() on the DbContext.

I am trying to save the following entity:

public class TVSeriesReference : Reference
{
}

TVSeriesReference does nothing but inherit Reference:

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 am using the following context:

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

And this is how I try to create and save the entity to the database.

using (var db = new Library())
{
    var reference = new TVSeriesReference
    {
        Id = 2,
        TheTVDBId = 1,
        IsWatched = true
     };

     db.TVSeriesReferences.Add(reference);

     try
     {
         db.SaveChanges();
     }
     catch (DbUpdateException e)
     {
         Debug.WriteLine("\n\n*** {0}\n\n", e.InnerException);
     }
 }

db.SaveChanges() will throw the following exception:

System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Invalid object name 'dbo.TVSeriesReferences'.
   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
   at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
   at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)
   at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
   at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)
   at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary`2 identifierValues, List`1 generatedValues)
   at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
   --- End of inner exception stack trace ---
   at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
   at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
   at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
   at System.Data.Entity.Internal.InternalContext.SaveChanges()

I have been tearing my hair out for hours and hours now and I can’t figure out what I am doing wrong. It seems that the tables (or maybe the entire database?) is not being created.
Is there something I should have set up or have installed before this will work? I have installed NuGet and through that the Entity Framework.

Is there anyone who can help me get this to work?

UPDATE: ConnectionStrings in app.config

<connectionStrings>
    <add name="Library"
         connectionString="Data Source=.\SQLEXPRESS"
         providerName="System.Data.SqlServerCe.4.0"/>
  </connectionStrings>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T04:32:14+00:00Added an answer on June 13, 2026 at 4:32 am

    While working with Entity Framework Code First you still need to set proper ConnectionString in your app.config file. For example if you want to have a local database in file, you can use embedded SQL Server CE 4 database engine (of course Code First can be used with many different databases including SQL Server, SQL Server Express and MySQL):

    <connectionStrings> 
        <add name="Library" connectionString="Data Source=|DataDirectory|CodeFirst.sdf" providerName="System.Data.SqlServerCe.4.0"/> 
    </connectionStrings>
    

    At run time, the DataDirectory substitution string is replaced with the appropriate directory path for the application’s current execution environment. Without the DataDirectory substitution string, your application would have to programmatically determine the location of the database file based on the current execution environment and then dynamically build the path used in the connection string.

    The Code First has also an auto-recreate functionality which is controlled by initialization strategy. The default initialization strategy is CreateDatabaseIfNotExists. With this strategy if your ConnectionString is pointing to database which doesn’t exists Code First will create the database for you, but if the database already exists Code First will not try to create it.

    The initialization strategy can be changed with Database.SetInitializer() method to one of the following:

    • DropCreateDatabaseAlways
    • DropCreateDatabaseIfModelChanges
    • MigrateDatabaseToLatestVersion

    You need to remember here, that recreating database will lead to losing all data that were stored there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a local database for my first Visual Studio project, I
I'm trying to create a billing database with Entity Framework 4.3 using Code First
I'm new to NHibernate and trying to create my first mapping. I have created
I'm trying to create a simple threading procedure (granted this is my first attempt
I am working in C# with Visual Studio 2008. I am trying to create
I'm trying to create a hypothetical videostore database and this error message comes up
I'm trying to create a user that can only read from a database. This
I'm new to objective c. I'm trying to create database for first time in
I'm trying to create a database model with Code First, without creating the database
I'm trying to create a efficient query which will retrieve 'projects' from a database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.