【问题标题】:DbContext.EnsureCreated() : ArgumentNullException only on RELEASE buildDbContext.EnsureCreated() : ArgumentNullException 仅在 RELEASE 构建时
【发布时间】:2020-07-31 18:23:30
【问题描述】:

我在我的xamarin-forms 应用程序中使用微软ef-coreSqlite 数据库。我的模型很大,所以我想把它放在这里不是一个好主意。我在该应用程序上工作了几周,并在我的手机上以 Debug 模式进行了测试 - 一切正常。

但是,一旦我构建了 RELEASE 版本 - 我就在 DbContext.EnsureCreated() 调用时得到了 ArgumentNullException。我可以使用控制台输出找到它。所以它说:

Value cannot be null. Parameter name: key

DEBUG 构建时,我的应用程序运行良好,而且我的代码中也没有依赖于构建的预编译器指令。使用 Visual Studio 2017

关于我可以检查什么的任何想法或建议?

UPD:这是我在 Release 中获得的堆栈跟踪:

I/mono-stdout(24851): TRACE:  at System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) [0x00008] in <d029cac6f9824b0bb72d5eb6d48d11f3>:0
I/mono-stdout(24851):   at System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) [0x00000] in <d029cac6f9824b0bb72d5eb6d48d11f3>:0
I/mono-stdout(24851):   at Microsoft.EntityFrameworkCore.Sqlite.Query.Internal.SqliteDateTimeAddTranslator..ctor (Microsoft.EntityFrameworkCore.Query.ISqlExpressionFactory sqlExpressionFactory) [0x000c5] in <7b7aa88a95d54aa786bada86edd4821a>:0
I/mono-stdout(24851):   at Microsoft.EntityFrameworkCore.Sqlite.Query.Internal.SqliteMethodCallTranslatorProvider..ctor (Microsoft.EntityFrameworkCore.Query.RelationalMethodCallTranslatorProviderDependencies dependencies) [0x0001d] in <7b7aa88a95d54aa786bada86edd4821a>:0
I/mono-stdout(24851):   at (wrapper managed-to-native) System.Reflection.MonoCMethod.InternalInvoke(System.Reflection.MonoCMethod,object,object[],System.Exception&)
I/mono-stdout(24851):   at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters, System.Boolean wrapExceptions) [0x00005] in <d029cac6f9824b0bb72d5eb6d48d11f3>:0

【问题讨论】:

    标签: sqlite xamarin.forms entity-framework-core


    【解决方案1】:

    这背后的原因是链接器过于激进,它从 mscorlib 程序集中剥离了 System.DateTime 类。正确的解决方法是创建一个Custom Linker Configuration,而不是您提到的 preserveDateTimeMethods 解决方案。

    1. 创建一个空文件LinkDescription.xml
    2. 将其添加到操作系统级项目(Android、iOS 等)。
    3. 将其构建操作设置为 LinkDescription
    4. System.DateTime 添加到链接器块

    方法如下:

    <?xml version="1.0" encoding="UTF-8" ?>
    <linker>
      <assembly fullname="mscorlib">
        <type fullname="System.DateTime" preserve="methods" />
      </assembly>
    </linker>
    

    【讨论】:

    • 谢谢!这对我有用,我想这确实是正确的做法。感谢您分享链接以及有关链接的更多信息。
    【解决方案2】:

    所以,我已经搜索了几个小时,并在我发布问题后立即......

    看起来这是一个 ef-core 错误 - 使用 DateTime 类型导致尝试将空键值添加到 Dictionary 的链接器问题。 错误讨论链接:

    https://github.com/dotnet/efcore/issues/14091

    https://github.com/dotnet/efcore/issues/10963

    我在我的 Android 版本构建中遇到了这个问题,所以我在第一次讨论时添加了来自其中一个 cmets 的代码行。

    在 Xamarin.Forms 初始化之前在我的 MainActivity.cs 中添加了这一行:

     var preserveDateTimeMethods = DateTime.Now.AddYears(1).AddMonths(1).AddDays(1).AddHours(1).AddMinutes(1).AddSeconds(1);
    

    它解决了我的问题。希望它可以帮助某人。

    【讨论】:

    • 感谢您的分享,请不要忘记在两天后标记您的答案,它将帮助其他有类似问题的人。
    猜你喜欢
    • 2017-05-28
    • 2010-12-08
    • 2021-06-09
    • 1970-01-01
    • 2014-09-05
    • 1970-01-01
    • 2017-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多