无法将类型为“ System.Data.SQLite.SQLiteConnection”的对象转换为类型为“ System.Data.SqlClient.SqlConnection”

时间:2018-09-26 18:32:59

标签: c# entity-framework sqlite entity-framework-6

尝试将SQLite与EF6结合使用,但在尝试执行GetMany类型的操作时收到错误Unable to cast object of type 'System.Data.SQLite.SQLiteConnection' to type 'System.Data.SqlClient.SqlConnection'.。安装的Nuget软件包是:

  • EF 6.2.0
  • System.Data.SQLite 1.0.109.1
  • System.Data.SQLite.Core 1.0.109.1
  • System.Data.SQLite.EF6 1.0.109
  • System.Data.SQLite.Linq 1.0.109

app.config     

<configuration>
  <configSections>
    <!-- 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>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </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" />
      <provider invariantName="System.Data.SQLite"
                type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="System.Data.SQLite.EF6" />
      <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6"
           description=".NET Framework Data Provider for SQLite (Entity Framework 6)"
           type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
      <remove invariant="System.Data.SQLite" />
      <add name="SQLite Data Provider" invariant="System.Data.SQLite"
           description=".NET Framework Data Provider for SQLite"
           type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
    </DbProviderFactories>
  </system.data>
</configuration>

dbContext

namespace EntityDataModel
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Infrastructure;

    public partial class SampleEmbeddedDBEntities : DbContext
    {
        public SampleEmbeddedDBEntities(EntityConnection existingConnection) : base(existingConnection, true)
        {
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            throw new UnintentionalCodeFirstException();
        }

        public virtual DbSet<Datum> Data { get; set; }
    }
}

控制台程序

using System;
using System.Configuration;
using System.Data.Entity.Core.EntityClient;
using System.Data.SQLite;
using System.Linq;
using EntityDataModel;

namespace EmbeddedDatabase
{
    class Program
    {
        static void Main(string[] args)
        {
            var sqLiteBuilder = new SQLiteConnectionStringBuilder
            {
                DataSource = "EmbeddedSample.db",
                Password = "password"
            };

            var entityBuilder = new EntityConnectionStringBuilder
            {
                Provider = "System.Data.SQLite.EF6",
                ProviderConnectionString = sqLiteBuilder.ToString(),
                Metadata =
                    @"res://*/SampleEntityDataModel.csdl|res://*/SampleEntityDataModel.ssdl|res://*/SampleEntityDataModel.msl"
            };

            var test = new SampleEmbeddedDBEntities(new EntityConnection(entityBuilder.ToString()));
            var stuff = test.Data.ToList();
        }
    }
}

0 个答案:

没有答案