sqlite EF DbUpdateException

用户名

我正在尝试使用下面定义的简单控制台应用程序来学习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](Func1个函式,位于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()(Func 1 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.InternalDispatcher1.Dispatch [TInterceptionContext,TResult](1 operation, TInterceptionContext interceptionContext, Action执行的Func 1 1 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:

ILOVECSHARP

我想您已经更改了模型,或者自第一次创建数据库后就添加了Blog。在BloggingContext的承包商中,编写以下代码:

Database.SetInitializer<BloggingContext>(new DropCreateDatabaseIfModelChanges<BloggingContext>());

希望它能工作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

sqlite EF DbUpdateException

来自分类Dev

尝试首先使用EF核心代码的DbUpdateException错误

来自分类Dev

如何从EF DbUpdateException获取一些其他信息

来自分类Dev

MVC EF - System.Data.Entity.Infrastructure.DbUpdateException

来自分类Dev

使用 SQLite EF 建模实体

来自分类Dev

查找DBUpdateException的原因

来自分类Dev

如何使用EF在SQLite中启用迁移

来自分类Dev

带有 EF Core 的沙盒 Sqlite

来自分类Dev

SaveChanges上的DbUpdateException不断抛出

来自分类Dev

WPF中带有EF和SQLite的多对多关系

来自分类Dev

System.Data.SQLite 1.0.91.0和EF6.0.2

来自分类Dev

EF7使用SQLite生成错误的迁移

来自分类Dev

Xamarin Forms,Sqlite,EF Core 3,迁移和大量混淆

来自分类Dev

System.Data.SQLite 1.0.91.0和EF6.0.2

来自分类Dev

如何使用 EF Core 更改 Sqlite 数据库?

来自分类Dev

Execute扩展会抛出DbUpdateException吗?

来自分类Dev

实体框架-添加实体时DbUpdateException

来自分类Dev

ASP.NET 5,EF 7和SQLite-SQLite错误1:“没有这样的表:博客”

来自分类Dev

EF 6和SQLite 1.0.96.0的“未找到实体框架提供程序”

来自分类Dev

SQLite EF6在运行时以编程方式设置连接字符串

来自分类Dev

EF7(代码优先)+ SQLite不会创建数据库和模型表

来自分类Dev

通用Windows App 10-EF7 SQLite一对多关系

来自分类Dev

无法使SQLite在带有EF6的VS2019中工作

来自分类Dev

无法为MySql DbContext在dotnet ef迁移中创建对象错误,但适用于Sqlite DbContext

来自分类Dev

SQLite内存数据库通过时态表测试EF Core应用程序

来自分类Dev

EF6首先使用SQLite模型创建数据库

来自分类Dev

在服务器上部署时,带有 EF6 的 Sqlite 不起作用

来自分类Dev

使用带有 EF Core 和 SQLite 的 EFE 无法解析提供程序异常

来自分类Dev

如何避免System.Data.Entity.Infrastructure.DbUpdateException

Related 相关文章

  1. 1

    sqlite EF DbUpdateException

  2. 2

    尝试首先使用EF核心代码的DbUpdateException错误

  3. 3

    如何从EF DbUpdateException获取一些其他信息

  4. 4

    MVC EF - System.Data.Entity.Infrastructure.DbUpdateException

  5. 5

    使用 SQLite EF 建模实体

  6. 6

    查找DBUpdateException的原因

  7. 7

    如何使用EF在SQLite中启用迁移

  8. 8

    带有 EF Core 的沙盒 Sqlite

  9. 9

    SaveChanges上的DbUpdateException不断抛出

  10. 10

    WPF中带有EF和SQLite的多对多关系

  11. 11

    System.Data.SQLite 1.0.91.0和EF6.0.2

  12. 12

    EF7使用SQLite生成错误的迁移

  13. 13

    Xamarin Forms,Sqlite,EF Core 3,迁移和大量混淆

  14. 14

    System.Data.SQLite 1.0.91.0和EF6.0.2

  15. 15

    如何使用 EF Core 更改 Sqlite 数据库?

  16. 16

    Execute扩展会抛出DbUpdateException吗?

  17. 17

    实体框架-添加实体时DbUpdateException

  18. 18

    ASP.NET 5,EF 7和SQLite-SQLite错误1:“没有这样的表:博客”

  19. 19

    EF 6和SQLite 1.0.96.0的“未找到实体框架提供程序”

  20. 20

    SQLite EF6在运行时以编程方式设置连接字符串

  21. 21

    EF7(代码优先)+ SQLite不会创建数据库和模型表

  22. 22

    通用Windows App 10-EF7 SQLite一对多关系

  23. 23

    无法使SQLite在带有EF6的VS2019中工作

  24. 24

    无法为MySql DbContext在dotnet ef迁移中创建对象错误,但适用于Sqlite DbContext

  25. 25

    SQLite内存数据库通过时态表测试EF Core应用程序

  26. 26

    EF6首先使用SQLite模型创建数据库

  27. 27

    在服务器上部署时,带有 EF6 的 Sqlite 不起作用

  28. 28

    使用带有 EF Core 和 SQLite 的 EFE 无法解析提供程序异常

  29. 29

    如何避免System.Data.Entity.Infrastructure.DbUpdateException

热门标签

归档