简体   繁体   中英

Having problem inserting child list of objects with included textblobs

I'm hoping someone can shed some light on this problem. Being new to the extensions, it is probably something I'm doing wrong. I have an object called Reason:

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

Then the MiniGroup object looks like this:

    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; }
    }

I create the two tables without difficulty. The problem is inserting data. I tried

        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);
                }

            }

        }

This code results in System.NullReferenceException: Object reference not set to an instance of object with the below stack trace. I've checked the object, the only null values are the blob targets. My understanding those get populated during the Insert.

  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 

I have even tried looping through the MiniGroup list and inserting each individually, that results in the same error.

Anyone see what I'm doing wrong? Any help is greatly appreciated!

So I solved the problem by simplifying the set up. Instead of having a OneToMany relationship from reason to groups, I made the groups into a TextBlob. Here are the working objects:

    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; }
    }

Then the processing becomes much simpler:

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


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


        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM