我正在尝试使用下面定义的简单控制台应用程序来学习SQLite和实体框架。当我在VS中运行此代码时,执行context.SaveChanges()时出现如下所示的异常。我需要解决此问题的帮助。
using System;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Reflection;
namespace EntityFrameworkConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var sb = new System.Data.SQLite.SQLiteConnectionStringBuilder();
sb.DataSource = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "test.db");
sb.ForeignKeys = true;
sb.BinaryGUID = true;
sb.DateTimeFormat = SQLiteDateFormats.ISO8601;
sb.DateTimeKind = DateTimeKind.Local;
sb.ToFullPath = true;
using (var context = new BloggingContext(new SQLiteConnection(sb.ConnectionString)))
{
context.Blogs.Add(new Blog { Name = "Yet Another Blog #1" });
context.SaveChanges();
var blogs = (from b in context.Blogs
orderby b.Name
select b).ToList();
}
}
}
}
using System.Data.Common;
using System.Data.Entity;
namespace EntityFrameworkConsoleApplication1
{
[DbConfigurationType(typeof(MyConfiguration))]
public class BloggingContext : DbContext
{
public BloggingContext(DbConnection c):base(c,true)
{
}
public DbSet<Blog> Blogs { get; set; }
}
}
namespace EntityFrameworkConsoleApplication1
{
public class Blog
{
public int BlogId { get; set; }
public string Name { get; set; }
}
}
using System;
using System.Data.Entity;
using System.Data.Entity.Core.Common;
using System.Data.SQLite;
using System.Data.SQLite.Linq;
using System.Reflection;
namespace EntityFrameworkConsoleApplication1
{
public class MyConfiguration : DbConfiguration
{
public MyConfiguration()
{
SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);
SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance);
Type t = Type.GetType(
"System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6");
FieldInfo fi = t.GetField("Instance", BindingFlags.NonPublic | BindingFlags.Static);
SetProviderServices("System.Data.SQLite", (DbProviderServices)fi.GetValue(null));
}
}
}
未处理System.Data.Entity.Infrastructure.DbUpdateException
_HResult = -2146233087 _message =更新条目时发生错误。有关详细信息,请参见内部异常。HResult = -2146233087
IsTransient = false消息=更新条目时发生错误。有关详细信息,请参见内部异常。Source = EntityFramework StackTrace:在System.Data.Entity.Internal.InternalContext.SaveChanges()在System.Data.Entity.Internal.LazyInternalContext.SaveChanges()在System.Data.Entity.DbContext.SaveChanges()在EntityFrameworkConsoleApplication1.Program。 c:\ Users \ John \ Documents \ Visual Studio 2013 \ Projects \ EntityFrameworkConsoleApplication1 \ EntityFrameworkConsoleApplication1 \ Program.cs:System.AppDomain._nExecuteAssembly中第33行的Main(String [] args)。 .AppDomain.ExecuteAssembly(String assemblyFile,Evidence assemblySecurity,String [] args)在Microsoft。VisualStudio.HostingProcess.HostProc.RunUsersAssembly()在System.Threading.ThreadHelper.ThreadStart_Context(对象状态)。在System.Threading.ExecutionContext.Run(ExecutionContext.Run(ExecutionContextexecutionContext,ContextCallback回调,对象状态,布尔保持同步Ctx)在System.Threading.ExecutionContext.RunInternal(ExecutionContext执行上下文,ContextCallback回调,对象状态,布尔keepSyncCtx) ,ContextCallback回调,对象状态),位于System.Threading.ThreadHelper.ThreadStart()内部异常:System.Data.Entity.Core.UpdateException _HResult = -2146233087 _message =更新条目时发生错误。有关详细信息,请参见内部异常。HResult = -2146233087 IsTransient = false消息=更新条目时发生错误。有关详细信息,请参见内部异常。Source = EntityFramework StackTrace:位于System.Data.Entity.Core.Mapping.Update.Internal.UpdateTranslator。2 updateFunction, Boolean throwOnClosedConnection) at System.Data.Entity.Core.EntityClient.Internal.EntityAdapter.Update(Boolean throwOnClosedConnection) at System.Data.Entity.Core.Objects.ObjectContext.<SaveChangesToStore>b__33() at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func
1个函式,位于System.Data.Entity.Core.Objects.ObjectContext.SaveChangesToStore(SaveOptions选项,IDbExecutionStrategy执行策略)位于System.Data.Entity.Core.Objects.ObjectContext。<> c__DisplayClass28。 System.Data.Entity.Infrastructure.DefaultExecutionStrategy.Execute的b__25()(Func1 operation) at System.Data.Entity.Core.Objects.ObjectContext.SaveChanges(SaveOptions options) at System.Data.Entity.Internal.InternalContext.SaveChanges() InnerException: System.Data.SQLite.SQLiteException _HResult=-2147467259 _message=SQL logic error or missing database no such table: Blogs HResult=-2147467259 IsTransient=false Message=SQL logic error or missing database no such table: Blogs Source=System.Data.SQLite ErrorCode=1 StackTrace: at System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous, UInt32 timeoutMS, String& strRemain) at System.Data.SQLite.SQLiteCommand.BuildNextCommand() at System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index) at System.Data.SQLite.SQLiteDataReader.NextResult() at System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave) at System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior) at System.Data.SQLite.SQLiteCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<>c__DisplayClassb.<Reader>b__8() at System.Data.Entity.Infrastructure.Interception.InternalDispatcher
1.Dispatch [TInterceptionContext,TResult](1 operation, TInterceptionContext interceptionContext, Action
执行的Func 11 executed) at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext) at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior) at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior) at System.Data.Entity.Core.Mapping.Update.Internal.DynamicUpdateCommand.Execute(Dictionary
,Action.2标识符值,List`1生成值)在System.Data.Entity.Core处.Mapping.Update.Internal.UpdateTranslator.Update()InnerException:
我想您已经更改了模型,或者自第一次创建数据库后就添加了Blog。在BloggingContext的承包商中,编写以下代码:
Database.SetInitializer<BloggingContext>(new DropCreateDatabaseIfModelChanges<BloggingContext>());
希望它能工作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句