【问题标题】:Having problem inserting child list of objects with included textblobs插入包含文本块的对象子列表时出现问题
【发布时间】:2020-04-25 17:59:30
【问题描述】:

我希望有人能对这个问题有所了解。作为扩展的新手,这可能是我做错了。我有一个名为 Reason 的对象:

    public class Reason
    {
        [PrimaryKey, AutoIncrement]
        public int id { get; set; }
        public string Description { get; set; }
        [OneToMany]
        public List<MiniGroup> Groups {get;set; }
    }

那么 MiniGroup 对象如下所示:

    public class MiniGroup
    {
        [AutoIncrement, PrimaryKey]
        public int id { get; set; }
        public string TeamNumber { get; set; }
        [TextBlob("guidBlog")]
        public List<string> StudentGuids { get; set; }
        [TextBlob("nameBlob")]
        public List<string> StudentNames { get; set; }
        public string guidBlob { get; set; }
        public string nameBlob { get; set; }
        [ForeignKey(typeof(Reason))]
        public int ReasonID { get; set; }
    }

我毫不费力地创建了这两个表。问题是插入数据。我试过了

        public async Task AddNewReasonsAsync(List<Reason> reasons)
        {
            foreach (Reason reason in reasons)
            {
                try
                {
                    await conn.InsertWithChildrenAsync(reason);
                    await conn.InsertAllWithChildrenAsync(reason.Groups, true);

                    StatusMessage = string.Format("Added [Name: {0})", reason.Groups.Count);
                }
                catch (Exception ex)
                {
                    // now, try to update
                    try
                    {
                        await conn.UpdateWithChildrenAsync(reason.Groups);
                        await conn.UpdateWithChildrenAsync(reason);
                    }
                    catch (Exception)
                    {

                        StatusMessage = string.Format("Failed to add {0}. Error: {1}", reason.Groups.Count, ex.Message);
                    }
                    StatusMessage = string.Format("Failed to add {0}. Error: {1}", reason.Groups.Count, ex.Message);
                }

            }

        }

此代码导致 System.NullReferenceException:对象引用未设置为具有以下堆栈跟踪的对象实例。我检查了对象,唯一的空值是 blob 目标。我的理解是在插入期间填充这些内容。

  at SQLiteNetExtensions.Extensions.TextBlob.TextBlobOperations.UpdateTextBlobProperty (System.Object element, System.Reflection.PropertyInfo relationshipProperty) [0x00041] in C:\projects\sqlite-net-extensions\SQLiteNetExtensions\Extensions\TextBlob\TextBlobOperations.cs:54 
  at SQLiteNetExtensions.Extensions.WriteOperations.RefreshForeignKeys (System.Object element) [0x000b8] in C:\projects\sqlite-net-extensions\SQLiteNetExtensions\Extensions\WriteOperations.cs:368 
  at SQLiteNetExtensions.Extensions.WriteOperations.UpdateWithChildren (SQLite.SQLiteConnection conn, System.Object element) [0x00000] in C:\projects\sqlite-net-extensions\SQLiteNetExtensions\Extensions\WriteOperations.cs:39 
  at SQLiteNetExtensions.Extensions.WriteOperations.InsertAllWithChildrenRecursive (SQLite.SQLiteConnection conn, System.Collections.IEnumerable elements, System.Boolean replace, System.Boolean recursive, System.Collections.Generic.ISet`1[T] objectCache) [0x0006d] in C:\projects\sqlite-net-extensions\SQLiteNetExtensions\Extensions\WriteOperations.cs:169 
  at SQLiteNetExtensions.Extensions.WriteOperations.InsertAllWithChildren (SQLite.SQLiteConnection conn, System.Collections.IEnumerable elements, System.Boolean recursive) [0x00000] in C:\projects\sqlite-net-extensions\SQLiteNetExtensions\Extensions\WriteOperations.cs:91 
  at SQLiteNetExtensionsAsync.Extensions.WriteOperations+<>c__DisplayClass3_0.<InsertAllWithChildrenAsync>b__0 () [0x00013] in C:\projects\sqlite-net-extensions\SQLiteNetExtensionsAsync\Extensions\WriteOperations.cs:103 
  at System.Threading.Tasks.Task.InnerInvoke () [0x0000f] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2476 
  at System.Threading.Tasks.Task.Execute () [0x00000] in /Users/builder/jenkins/workspace/archive-mono/2019-10/android/release/external/corert/src/System.Private.CoreLib/src/System/Threading/Tasks/Task.cs:2319 
--- End of stack trace from previous location where exception was thrown ---

  at StudentGroupsMobile.Data.ReasonDBManager.AddNewReasonsAsync (System.Collections.Generic.List`1[T] reasons) [0x00216] in C:\Users\toman\source\repos\StudentGroupsMobile_XF\StudentGroupsMobile\Data\ReasonDBManager.cs:37 

我什至尝试循环遍历 MiniGroup 列表并单独插入每个列表,这会导致相同的错误。

有人看到我做错了吗?非常感谢任何帮助!

【问题讨论】:

  • 您发布了堆栈跟踪,但实际的异常是什么?
  • 对不起。它是 System.NullReferenceException: Object reference not set to an instance of object.
  • 你能分享一下你的 ReasonDBManager 类中 AddNewReasonsAsync 函数的代码吗?看起来你正试图传递一个 null 的原因,它不应该为 null。如果你只是放置空检查,你应该能够修复它。
  • 感谢您的回复,我已将 AddNewReasons 添加到上面的帖子中。我还检查过,原因对象(父对象)存在并成功添加,尝试添加子对象失败。我也查过了,孩子们在那里,他们不是空的。
  • 请在sqlite DB中打开该表,检查该列是否存在。

标签: c# android xamarin sqlite-net sqlite-net-extensions


【解决方案1】:

所以我通过简化设置解决了这个问题。我没有将原因与组建立 OneToMany 关系,而是将组变成了一个 TextBlob。以下是工作对象:

    public class Reason
    {
        [PrimaryKey, AutoIncrement]
        public int id { get; set; }
        public string Description { get; set; }
        [TextBlob("GroupsBlob")]
        public List<MiniGroup> Groups {get;set; }
        public string GroupsBlob { get; set; }
    }

    public class MiniGroup
    {
        [AutoIncrement, PrimaryKey]
        public int id { get; set; }
        public string TeamNumber { get; set; }
        [TextBlob("guidBlog")]
        public List<string> StudentGuids { get; set; } //= new List<string>();
        [TextBlob("nameBlob")]
        public List<string> StudentNames { get; set; } //= new List<string>();
        public string guidBlob { get; set; }
        public string nameBlob { get; set; }
        //[ForeignKey(typeof(Reason))]
        //public int ReasonID { get; set; }
    }

那么处理就变得简单多了:

        public async Task AddNewReasonsAsync(List<Reason> reasons)
        {


            try
            {
                await conn.InsertAllWithChildrenAsync(reasons);
            }
            catch (Exception)
            {
                await conn.UpdateWithChildrenAsync(reasons);
            }


        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 2020-07-26
    • 1970-01-01
    • 1970-01-01
    • 2015-05-17
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多