【问题标题】:EF 6 with System.Data.SQlite SaveChanges() not working?带有 System.Data.SQlite SaveChanges() 的 EF 6 不起作用?
【发布时间】:2014-02-18 05:29:31
【问题描述】:

所以我刚开始使用带有实体框架 6 的 System.Data.Sqlite(从 Nuget 下载了最新的 System.Data.Sqlite,版本 1.0.91.0) 经过一些配置和代码,我发现我可以从数据库中读取,但不知何故写入不起作用。

这是我的代码:

using (var context = new InternalDbContext())
        {
            var demo = context.DemoEntities.Where(d => d.ID == 1).FirstOrDefault();
            demo.Name = "TestTest";
            context.DemoEntities.Add(new Demo { Name = "Test" });
            context.SaveChanges();
        }

基本上在 SaveChanges 之后,数据库中没有任何更新。但是,我可以使用通过 SQlite 管理工具手动填充的数据来读取数据库。

这是我的数据库架构: Table name :Demo Field: ID - Integer Primary Key AutoIncrement Field: Name - VARCHAR(256)

这是我的课程


public class InternalDbContext : DbContext
{
    public DbSet<Demo> DemoEntities { get; set; }

    public InternalDbContext()
    {
        // Turn off the Migrations, (NOT a code first Db)
        Database.SetInitializer<InternalDbContext>(null);
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // Database does not pluralize table names
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    }
}


[Table("Demo")]
public class Demo
{
    public long ID { get; set; }
    public string Name { get; set; }
}

App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <sectionGroup name="common">
      <section name="logging" type="Common.Logging.ConfigurationSectionHandler, Common.Logging" />
    </sectionGroup>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <common>
    <logging>
      <factoryAdapter type="Common.Logging.Log4Net.Log4NetLoggerFactoryAdapter, Common.Logging.Log4net">
        <arg key="configType" value="FILE-WATCH" />
        <arg key="configFile" value="log4net.config" />
      </factoryAdapter>
    </logging>
  </common>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Common.Logging" publicKeyToken="af08829b84f0328e" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-2.0.0.0" newVersion="2.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
    <assemblyIdentity name="log4net" publicKeyToken="669e0ddf0bb1aa2a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.2.13.0" newVersion="1.2.13.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    </providers>
  </entityFramework>
  <connectionStrings>
    <add name="InternalDbContext" connectionString="Data Source=.\testdb.sqlite" providerName="System.Data.SQLite.EF6" />
  </connectionStrings>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite.EF6" />
      <add name="SQLite Data Provider" invariant="System.Data.SQLite.EF6" description="Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
    </DbProviderFactories>
  </system.data>
</configuration>

如果有人能指出正确的方向,那就太好了,非常感谢

尼克

【问题讨论】:

  • 您能否检查任何 SQLLite 日志以查看 a) 请求是否命中数据库以及 b) 是否生成了一些错误?
  • 我不知道SQLite的日志通常在哪里生成。你有什么指示吗?谢谢!

标签: c# sql entity-framework sqlite system.data.sqlite


【解决方案1】:

您的 ./bin/Debug/ 文件夹应该包含您的 testdb.sqlite DB 的副本。那应该有变化。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-16
    • 1970-01-01
    • 1970-01-01
    • 2015-06-11
    • 2019-02-01
    • 1970-01-01
    相关资源
    最近更新 更多