2014-02-18 45 views
0

因此,我剛剛開始使用System.Data.Sqlite與實體框架6(從Nuget下載最新的System.Data.Sqlite,版本1.0.91.0) 經過一些配置和代碼後,我發現我可以從數據庫中讀取但不知何故寫不起作用。EF 6與System.Data.SQlite SaveChanges()不起作用?

這裏是我的代碼:

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管理工具手動填充數據來讀取數據庫。

這裏是我的DB模式: 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> 

如果任何人都可以點我到正確的方向,即」 d太棒了,非常感謝

尼克

+0

您可以檢查任何SQLLite日誌,以查看a)是否請求觸及數據庫和b)是否生成了一些錯誤? –

+0

我不知道通常爲SQLite生成日誌的位置。你有什麼建議嗎?謝謝! – littlejedi

回答

1

您的./bin/Debug/文件夾應包含testdb.sqlite數據庫的副本。這應該有變化。

+0

這是正確的。 –