Skip to content

Instantly share code, notes, and snippets.

@0xced
Last active November 15, 2024 17:17
Show Gist options
  • Save 0xced/701bcfec9c54bc0c151a365923b74e69 to your computer and use it in GitHub Desktop.
Save 0xced/701bcfec9c54bc0c151a365923b74e69 to your computer and use it in GitHub Desktop.
Single exe using Microsoft.EntityFrameworkCore.Sqlite on .NET Framework 4.7.2
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>exe</OutputType>
<TargetFramework>net472</TargetFramework>
<DebugType>embedded</DebugType>
<AutoGenerateBindingRedirects>false</AutoGenerateBindingRedirects>
<GenerateSupportedRuntime>false</GenerateSupportedRuntime>
<LangVersion>8</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Costura.Fody" Version="5.7.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.32" />
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106" PrivateAssets="all" />
<PackageReference Include="SQLitePCLRaw.provider.dynamic_cdecl" Version="2.1.10" />
<PackageReference Include="SQLitePCLRaw.lib.e_sqlite3" Version="2.1.10" GeneratePathProperty="true" />
</ItemGroup>
<Target Name="EmbedNativeSQLiteDllWithCostura" BeforeTargets="ResolveAssemblyReferences">
<ItemGroup>
<EmbeddedResource Include="$(PkgSQLitePCLRaw_lib_e_sqlite3)\runtimes\win-x86\native\e_sqlite3.dll">
<Link>costura32\e_sqlite3.dll</Link>
<Visible>false</Visible>
</EmbeddedResource>
<EmbeddedResource Include="$(PkgSQLitePCLRaw_lib_e_sqlite3)\runtimes\win-x64\native\e_sqlite3.dll">
<Link>costura64\e_sqlite3.dll</Link>
<Visible>false</Visible>
</EmbeddedResource>
<Content Remove="@(Content)" Condition="'%(Filename)%(Extension)' == 'e_sqlite3.dll'" />
<ReferenceCopyLocalPaths Remove="@(ReferenceCopyLocalPaths)" Condition="'%(Filename)%(Extension)' == 'e_sqlite3.dll' OR '%(Filename)%(Extension)' == 'SQLitePCLRaw.batteries_v2.dll'" />
</ItemGroup>
</Target>
</Project>
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
<Costura />
</Weavers>
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using SQLitePCL;
using Windows.Win32;
using Microsoft.Win32.SafeHandles;
namespace efsqlite
{
public class ModuleGetFunctionPointer : IGetFunctionPointer
{
private readonly ProcessModule _module;
public static ProcessModule GetModule(string moduleName)
{
var modules = Process.GetCurrentProcess().Modules.Cast<ProcessModule>().Where(e => Path.GetFileNameWithoutExtension(e.ModuleName) == moduleName).ToList();
if (modules.Count == 0)
{
throw new ArgumentException($"Found no modules named '{moduleName}' in the current process.", nameof(moduleName));
}
if (modules.Count > 1)
{
throw new ArgumentException($"Found several modules named '{moduleName}' in the current process.", nameof(moduleName));
}
return modules[0];
}
public ModuleGetFunctionPointer(string moduleName) : this(GetModule(moduleName))
{
}
public ModuleGetFunctionPointer(ProcessModule module)
{
_module = module ?? throw new ArgumentNullException(nameof(module));
}
public IntPtr GetFunctionPointer(string name)
{
using var handle = new SafeProcessHandle(_module.BaseAddress, ownsHandle: false);
return PInvoke.GetProcAddress(handle, name);
}
}
}
using System;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using SQLitePCL;
namespace efsqlite
{
class Program
{
static async Task<int> Main()
{
try
{
const string name = "e_sqlite3";
SQLite3Provider_dynamic_cdecl.Setup(name, new ModuleGetFunctionPointer(name));
raw.SetProvider(new SQLite3Provider_dynamic_cdecl());
var options = new DbContextOptionsBuilder().UseSqlite("Data Source=:memory:").Options;
await using var context = new DbContext(options);
await context.Database.EnsureCreatedAsync();
Console.WriteLine($"Microsoft.EntityFrameworkCore.Sqlite is working with Costura and {raw.GetNativeLibraryName()}.");
return 0;
}
catch (Exception exception)
{
Console.Error.WriteLine(exception);
return 1;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment