.NET MAUI iOS下Entity Framework Core的裁剪与链接器配置问题
.NET MAUI iOS Release构建启动崩溃(EF Core相关)
问题背景
将使用Entity Framework Core 3.1 + SQLite的Xamarin Forms 5.0应用迁移至.NET MAUI 6.0 + EF Core 6.0后,Debug构建正常,但iOS的Release构建启动即崩溃,Android Release构建无异常。
复现步骤
- 在Visual Studio中创建.NET MAUI App模板项目,编辑项目文件添加以下配置以支持
dotnet build:
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Debug|net6.0-ios|AnyCPU'"> <CreatePackage>false</CreatePackage> <CodesignProvision>Automatic</CodesignProvision> <CodesignKey>iPhone Developer</CodesignKey> <RuntimeIdentifier>ios-arm64</RuntimeIdentifier> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net6.0-ios|AnyCPU'"> <CreatePackage>false</CreatePackage> <CodesignProvision>Automatic</CodesignProvision> <CodesignKey>iPhone Distribution</CodesignKey> <RuntimeIdentifier>ios-arm64</RuntimeIdentifier> </PropertyGroup>
- 终端执行以下命令运行Release构建(替换项目名和设备UDID):
dotnet build MauiEFCore.csproj -c Release -f net6.0-ios -t:Run -p:_DeviceName=<Device UDID>
此时应用可正常运行;但添加EF Core相关包后启动崩溃:
<ItemGroup> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.11" /> <PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.3" /> </ItemGroup>
错误现象
启动后控制台输出包含AOT加载错误,随后触发SIGABRT崩溃:
Launched application 'com.companyname.mauiefcore' on 'Gerwin's iPhone 11' with pid 25752 2023-01-06 16:00:53.767 MauiEFCore[25752:10999573] error: Failed to load AOT module 'Microsoft.Maui.Graphics' while running in aot-only mode because a dependency cannot be found or it is out of date. ================================================================= Native Crash Reporting ================================================================= Got a SIGABRT while executing native code. This usually indicates a fatal error in the mono runtime or one of the native libraries used by your application. ================================================================= // 省略冗长的Native栈追踪... Application 'com.companyname.mauiefcore' terminated (with exit code '' and/or crashing signal '6).
已尝试的无效操作
- 添加
<MtouchLink>None</MtouchLink>禁用链接,但构建仍显示正在进行程序集大小优化(裁剪),崩溃未解决 - 尝试关闭裁剪(设置
<PublishTrimmed>false</PublishTrimmed>),但触发错误:iOS projects must build with PublishTrimmed=true - 在Xamarin Forms中曾使用的
--linkskip System.Core参数在MAUI中被忽略,构建日志显示Skipping unknown argument 'linkskip' - 添加EF Core相关程序集(
Microsoft.EntityFrameworkCore.Abstractions、SQLitePCLRaw.provider.internal)的链接保留设置,无效
解决方案建议
针对.NET MAUI iOS的裁剪/AOT特性,需通过以下配置确保EF Core相关组件不被错误裁剪:
1. 添加链接器描述文件
在项目中创建linker.xml文件,内容如下:
<linker> <assembly fullname="Microsoft.EntityFrameworkCore"> <type fullname="*" /> </assembly> <assembly fullname="Microsoft.EntityFrameworkCore.Sqlite"> <type fullname="*" /> </assembly> <assembly fullname="SQLitePCLRaw.core"> <type fullname="*" /> </assembly> <assembly fullname="SQLitePCLRaw.provider.sqlite3"> <type fullname="*" /> </assembly> <assembly fullname="Microsoft.EntityFrameworkCore.Abstractions"> <type fullname="*" /> </assembly> </linker>
并在项目文件中引用该文件:
<ItemGroup> <LinkerDescriptor Include="linker.xml" Condition="'$(TargetFramework)' == 'net6.0-ios'" /> </ItemGroup>
2. 调整AOT和裁剪参数
在Release的iOS属性组中添加以下配置,确保EF Core的动态特性被兼容:
<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net6.0-ios|AnyCPU'"> <!-- 保留原有配置 --> <CreatePackage>false</CreatePackage> <CodesignProvision>Automatic</CodesignProvision> <CodesignKey>iPhone Distribution</CodesignKey> <RuntimeIdentifier>ios-arm64</RuntimeIdentifier> <!-- 添加以下配置 --> <PublishTrimmed>true</PublishTrimmed> <TrimMode>partial</TrimMode> <AotAssemblies>true</AotAssemblies> <EnableLLVM>true</EnableLLVM> <!-- 排除EF相关程序集的裁剪 --> <TrimmerRootAssembly Include="Microsoft.EntityFrameworkCore" /> <TrimmerRootAssembly Include="Microsoft.EntityFrameworkCore.Sqlite" /> <TrimmerRootAssembly Include="SQLitePCLRaw.bundle_e_sqlite3" /> </PropertyGroup>
3. 更新依赖包版本
尝试将EF Core和SQLitePCLRaw包更新至.NET 6的最新稳定版本,确保与MAUI的兼容性:
<ItemGroup> <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.25" /> <PackageReference Include="SQLitePCLRaw.bundle_e_sqlite3" Version="2.1.6" /> </ItemGroup>
4. 验证SQLite原生库集成
确保SQLitePCLRaw的原生库被正确嵌入,可添加以下配置强制嵌入:
<ItemGroup Condition="'$(TargetFramework)' == 'net6.0-ios'"> <NativeReference Include="$(NuGetPackageRoot)sqlitepclraw.lib.e_sqlite3/2.1.6/runtimes/ios-arm64/native/libe_sqlite3.a" /> </ItemGroup>
内容的提问来源于stack exchange,提问作者Glorfindel
相关产品推荐
相关产品推荐

