简体   繁体   English

带有System.Data.SQlite SaveChanges()的EF 6无法正常工作?

[英]EF 6 with System.Data.SQlite SaveChanges() not working?

So I just started using System.Data.Sqlite with entity framework 6 (downloaded the latest System.Data.Sqlite from Nuget, version 1.0.91.0) After some configuration and code, I found out that I can read from the database but somehow write is not working. 因此,我刚刚开始将System.Data.Sqlite与实体框架6一起使用(从Nuget下载了最新的System.Data.Sqlite,版本1.0.91.0)经过一些配置和代码,我发现我可以从数据库中读取,但是以某种方式写入不管用。

Here's my code: 这是我的代码:

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

Basically after SaveChanges, nothing was updated in the DB. 基本上在SaveChanges之后,数据库中没有任何更新。 However I can read fro the DB with the data I manually populated via SQlite admin tool. 但是,我可以使用通过SQlite管理工具手动填充的数据来回读取数据库。

Here's my DB schema: Table name :Demo Field: ID - Integer Primary Key AutoIncrement Field: Name - VARCHAR(256) 这是我的数据库模式: Table name :Demo Field: ID - Integer Primary Key AutoIncrement Field: Name - VARCHAR(256)

Here's my classes 这是我的课


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

If anyone could point me to the right direction, that'd be fantastic, thanks so much 如果有人能指出我正确的方向,那就太好了,非常感谢

Nick 缺口

Your ./bin/Debug/ folder should contain a copy of your testdb.sqlite DB. 您的./bin/Debug/文件夹应包含testdb.sqlite数据库的副本。 That should have the changes. 那应该有变化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM