簡體   English   中英

插入包含文本塊的對象子列表時出現問題

[英]Having problem inserting child list of objects with included textblobs

我希望有人可以對這個問題有所了解。 作為擴展的新手,這可能是我做錯了。 我有一個名為原因的 object:

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

然后 MiniGroup object 看起來像這樣:

    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: Object reference not set to an instance of object with the following stack trace. 我檢查了 object,唯一的 null 值是 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 列表並單獨插入每個列表,這會導致相同的錯誤。

有人看到我做錯了嗎? 任何幫助是極大的贊賞!

所以我通過簡化設置解決了這個問題。 我沒有將原因與組建立 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);
            }


        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM