entity-framework - Xamarin iOS : Value cannot be null. 参数名称:key

标签 entity-framework xamarin.forms xamarin.ios .net-standard ef-core-2.1

我有一个 xamarin forms 解决方案,除了物理 iOS 设备之外,一切都正常运行。它可以 100% 在模拟 iOS、Android 和物理 Android 设备上运行,但不能在物理 iOS 设备上运行。

让我详细说明一下。该解决方案在物理 iOS 设备上运行,并进入登录屏幕,然后我在其中连接到数据库并登录。这适用于除物理 iOS 设备之外的所有设备。

我得到的错误是:

The type initializer for 'Microsoft.EntityFrameworkCore.SqlServer.Query.ExpressionTranslators.Internal.SqlServerCompositeMethodCallTranslator' threw an exception.

内部异常:

Value cannot be null.
Parameter name: key

这是堆栈跟踪:

  at System.Collections.Generic.Dictionary`2[TKey,TValue].TryInsert (TKey key, TValue value, System.Collections.Generic.InsertionBehavior behavior) [0x00008] in <c4da4bcb0a614f31bf9f25261a36b747>:0 
  at System.Collections.Generic.Dictionary`2[TKey,TValue].Add (TKey key, TValue value) [0x00000] in <c4da4bcb0a614f31bf9f25261a36b747>:0 
  at Microsoft.EntityFrameworkCore.SqlServer.Query.ExpressionTranslators.Internal.SqlServerDateAddTranslator..ctor () [0x000c5] in <841b527f2aa54cccaf5150a4cca376c9>:0 
  at Microsoft.EntityFrameworkCore.SqlServer.Query.ExpressionTranslators.Internal.SqlServerCompositeMethodCallTranslator..cctor () [0x00016] in <841b527f2aa54cccaf5150a4cca376c9>:0 

我正在使用 Entity Framework Core 2.1.2,我的数据项目中的目标框架是 .NET Standard 2.0

  1. 为什么物理 iOS 设备与模拟设备不同?

  2. 我该怎么做才能克服这个错误?

  3. 我是否可以找出它发现的值为空的值?

编辑

我发现我的问题是我没有连接到我的数据库。虽然我无法弄清楚我的连接出了什么问题。

这种连接sql服务器的方法有什么问题吗?

(注意:开发和主数据库是正确的,因为其他一切都可以使用 这些连接字符串)

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);

            //optionsBuilder.UseSqlServer(
            //    "System.Data.SqlClient",
            //    options => options.EnableRetryOnFailure(1, TimeSpan.FromSeconds(30), null));

            string cn;

            if(Debugger.IsAttached)
            {
                cn = devDB;
            }
            else
            {
                cn = mainDB;
            }

            SqlConnectionStringBuilder cns = new SqlConnectionStringBuilder();

            cns.ConnectionString = cn;

            optionsBuilder.UseSqlServer(cns.ConnectionString, options => options.EnableRetryOnFailure(1, TimeSpan.FromSeconds(60), null));
        }

最佳答案

Why would a physical iOS Device be different from a emulated one?

答案就在你的问题中:) 首先 - iOS 使用模拟器,而不是模拟器。 :) 不同之处在于模拟器不会尝试模拟设备的真实性能(时钟速度、I/O 等)。它会分配设备的 RAM(就像在 iPhone 5S 上一样,它会显示最大 RAM 为 512MB,但在 iPhone X 中为 2 GB)。但除此之外,它会充分利用机器的性能(例如 CPU 或写入/读取速度)。

无论如何,主要问题是为了在模拟器中运行应用程序,您需要为 x86 架构构建所有框架。那就是问题所在。有时人们不会为移动或桌面架构构建库。您很幸运,到目前为止您没有遇到这个问题:)))构建过程的差异可能会引入不可预测的错误,因此始终必须在设备上进行测试。

What can I do to get past this error?

很难说清楚,因为错误消息非常笼统。我个人首先要确保设备上存在数据库,并且我的代码至少可以打开与它的连接。

Is there anyway that I can find out what value it is finding to be null?

这是与插入相关的东西。也许,也许您一直在从数据库中读取数据,现在才尝试插入。如果是,那么您从位于 Bundle 中的数据库中读取数据。您需要将其复制到文档文件夹中才能写入。

希望对您有帮助!

关于entity-framework - Xamarin iOS : Value cannot be null. 参数名称:key,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51972596/

相关文章:

asp.net-mvc - 使用 Entity Framework ,如何访问外键列的显示名称?

android - Xamarin.Forms Android 沉浸式模式,带软键盘或警报

xamarin.ios - Xamarin Forms IOS 的绑定(bind)问题和 OnAppearing 问题

c# - 集合上的动态 LINQ?

c# - 从 Entity Framework Core 中的导航属性选择对象列表

icons - 图标大小 - Xamarin.forms

xamarin.ios - 可移植类库 System.Object 错误

c# - Xamarin.iOS 中 Swift 的 URL 结构的 C# 等效项是什么?

linq - Entity Framework 仅返回一个值但列表大小正确

c# - 使用 SELECT 查询时抛出异常 : 'System.Collections.Generic.KeyNotFoundException' in mscorlib. dll