我在使用SQLite作为数据库提供程序的应用程序中使用Entity Framework Core 2。
我在项目中添加了对以下内容的引用:
Microsoft.EntityFrameworkCore
Microsoft.EntityFrameworkCore.Sqlite.Core
Microsoft.Data.Sqlite
SQLitePCLRaw.batteries_green
SQLitePCLRaw.batteries_v2
SQLitePCLRaw.core
SQLitePCLRaw.provider.e_sqlite3
我试图在dbcontext上使用IMigrator
来设置我的初始数据库,如下所示:
using (var dbContext = new DbContext())
{
var migrator = dbContext.GetInfrastructure().GetRequiredService<IMigrator>();
migrator.Migrate();
}
在我的
DbContext
中,我设置使用以下命令添加服务:options.UseSqlite(connectionString);
我的连接字符串只是本地路径。
一切似乎都像我应该按照Microsoft文档所做的那样。但是,当我尝试运行应用程序时,出现以下错误:
'这是'诱饵'。您可能需要添加以下内容之一
SQLitePCLRaw.bundle_ * nuget包添加到您的平台项目。”
据我所知,由于我同时引用了
SQLitePCLRaw.bundle_*
和SQLitePCLRaw.batteries_green
,所以已经添加了SQLitePCLRaw.batteries_v2
程序包。有谁知道解决此错误并使用SQLite作为我的数据库提供程序的方法?
编辑
我的数据项目.csproj(略作编辑):
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{EA5DB634-7838-4C96-8DF5-4051B31DFFD2}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Data</RootNamespace>
<AssemblyName>Data</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>..\Build\Debug\bin\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\Build\Release\bin\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x64</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<DebugSymbols>true</DebugSymbols>
<OutputPath>..\Build\Debug\bin\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<OutputPath>..\Build\Release\bin\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<Optimize>true</Optimize>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.Data.Sqlite">
<HintPath>..\References\Microsoft.Data.Sqlite.Core.2.0.0\lib\netstandard2.0\Microsoft.Data.Sqlite.dll</HintPath>
</Reference>
<Reference Include="Microsoft.EntityFrameworkCore">
<HintPath>..\References\Microsoft.EntityFrameworkCore.2.0.0\lib\netstandard2.0\Microsoft.EntityFrameworkCore.dll</HintPath>
</Reference>
<Reference Include="Microsoft.EntityFrameworkCore.Relational">
<HintPath>..\References\Microsoft.EntityFrameworkCore.Relational.2.0.0\lib\netstandard2.0\Microsoft.EntityFrameworkCore.Relational.dll</HintPath>
</Reference>
<Reference Include="Microsoft.EntityFrameworkCore.Sqlite.Core">
<HintPath>..\References\Microsoft.EntityFrameworkCore.Sqlite.Core.2.0.0\lib\netstandard2.0\Microsoft.EntityFrameworkCore.Sqlite.dll</HintPath>
</Reference>
<Reference Include="Microsoft.EntityFrameworkCore.SqlServer">
<HintPath>..\References\Microsoft.EntityFrameworkCore.SqlServer.2.0.0\lib\netstandard2.0\Microsoft.EntityFrameworkCore.SqlServer.dll</HintPath>
</Reference>
<Reference Include="SQLitePCLRaw.batteries_green, Version=1.0.0.0, Culture=neutral, PublicKeyToken=a84b7dcfb1391f7f, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\References\SQLitePCLRaw.bundle_green.1.1.7\lib\net45\SQLitePCLRaw.batteries_green.dll</HintPath>
</Reference>
<Reference Include="SQLitePCLRaw.batteries_v2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=8226ea5df37bcae9, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\References\SQLitePCLRaw.bundle_green.1.1.7\lib\net45\SQLitePCLRaw.batteries_v2.dll</HintPath>
</Reference>
<Reference Include="SQLitePCLRaw.core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1488e028ca7ab535, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\References\SQLitePCLRaw.core.1.1.7\lib\net45\SQLitePCLRaw.core.dll</HintPath>
</Reference>
<Reference Include="SQLitePCLRaw.provider.e_sqlite3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9c301db686d0bd12, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\References\SQLitePCLRaw.provider.e_sqlite3.net45.1.1.7\lib\net45\SQLitePCLRaw.provider.e_sqlite3.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Collections.Immutable, Version=1.4.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\References\System.Collections.Immutable.1.4.0\lib\netstandard2.0\System.Collections.Immutable.dll</HintPath>
</Reference>
<Reference Include="System.ComponentModel.Annotations">
<HintPath>..\References\System.ComponentModel.Annotations.4.4.0\lib\netstandard2.0\System.ComponentModel.Annotations.dll</HintPath>
</Reference>
<Reference Include="System.Configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Data.SQLite, Version=1.0.104.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139, processorArchitecture=AMD64">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\References\System.Data.SQLite.1.0.104.0\System.Data.SQLite.dll</HintPath>
</Reference>
<Reference Include="System.Interactive.Async, Version=3.1.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\References\System.Interactive.Async.3.1.1\lib\netstandard1.3\System.Interactive.Async.dll</HintPath>
</Reference>
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Sqlite\SqliteDbConnectionFactory.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
我的DbContext在引用我的数据项目的另一个项目中。
SqliteDbConnectionFactory
通过我的DbContext中的UseSqlite
替代在DbContextOptionsBuilder
上设置OnConfiguring
。 最佳答案
因此,通过明智地使用反编译器,事实证明,NetStandard1.1构建的SQLitePCLRaw.batteries_v2.dll
程序集在编译时就将诱饵异常放入其中,而net45版本正确调用了SQLite提供程序初始化程序。
因为所有新的Entity Framework和AspNetcore程序集都使用某种NetStandard风格,所以我起初就一直使用NetStandard程序集,因此当我更改为net45来测试不同的配置时,由于忽略了bin文件夹而被删除,因此与不同版本发生冲突。
清除bin文件夹,仅引用net45版本的SQLitePCLRaw程序集即可初始化提供程序。
我遇到的下一个错误是找不到e_sqlite.dll
。这是在SQLitePCLRaw.lib.e_sqlite3.*
NuGet软件包之一中找到的非托管程序集。我下载了SQLitePCLRaw.lib.e_sqlite3.v110_xp
包,并将e_sqlite.dll
作为资源添加到我的项目中,并在运行时复制到我的bin目录中。
将e_sqlite.dll
放入容器后,项目将成功启动并创建数据库。
关于c# - '这是'诱饵'。您可能需要添加SQLitePCLRaw.bundle_ * nuget包之一,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/46559954/