我正在尝试在 Visual Studio C# 项目中创建我的第一个数据库并向该数据库添加一个实体。我还没有做到这一点。尝试时,我会在DbUpdateException
调用.SaveChanges()
DbContext
我正在尝试保存以下实体:
public class TVSeriesReference : Reference
{
}
TVSeriesReference
除了继承什么都不做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; }
}
我正在使用以下上下文:
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; }
}
这就是我尝试创建实体并将其保存到数据库的方式。
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()
将抛出以下异常:
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()
我已经把头发扯了好几个小时了,我不知道我做错了什么。似乎没有创建表(或者可能是整个数据库?)。在此之前我应该设置或安装一些东西吗?我已经安装了 NuGet 并通过它安装了实体框架。
有没有人可以帮助我让它工作?
更新:连接字符串在app.config
<connectionStrings>
<add name="Library"
connectionString="Data Source=.\SQLEXPRESS"
providerName="System.Data.SqlServerCe.4.0"/>
</connectionStrings>