Need help to save/retrive/update dictionary<string,object> to the SQL lite DB.

Sunil A M 171 Reputation points
2023-07-13T11:35:03.0266667+00:00

Hi Team,

I need some help to save the dictionary<string,object> to the sqllite DB.

could you please provide some sample code.

Example : Referenced Microsoft.EntityFrameworkCore.Sqlite nuget package

public class FXDBContext : DbContext
    {
        private const string FXDBPATH = @"\Sunil";
        private const string FXDBNAME = "sample.db";
        private static string DbPath { get; set; }

        #region Database Tables
        public DbSet<Dictionary<string,object>> KeybindString { get; set; }
        #endregion


        public FXDBContext()
        {
            try
            {
                if (DbPath == null)
                {
                    //FXLogger.Logger.Write("DataBase Initialization");
                    var appDataFolder = Environment.SpecialFolder.LocalApplicationData;
                    string databasePath = $@"{Environment.GetFolderPath(appDataFolder)}{FXDBPATH}";
                    DbPath = System.IO.Path.Join(databasePath, FXDBNAME);
                    if (!FXDBBuilder.IsDBPathExists(databasePath, FXDBNAME))
                    {
                        FXDBBuilder.CreateDBFolder(databasePath, FXDBNAME);
                    }
                    CreateDatabase();
                }
            }
            catch (Exception ex)
            {
                //FXLogger.Logger.Write(ex);
            }
        }
        
        void CreateDatabase()
        {
            DatabaseFacade databaseFacade = new DatabaseFacade(this);
            databaseFacade.EnsureCreated();
        }
		
		//How to save dictionary<string,object> to the sqllite db?
		//think that i am calling this savedata from outside
		public void savedata()
		{
		}
    }

Best Regards,

Sunil

Developer technologies | .NET | Entity Framework Core
Developer technologies | C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Wenbin Geng 741 Reputation points Microsoft External Staff
    2023-07-14T10:12:03.8833333+00:00

    Hi @Sunil A M , Welcome to Microsoft Q&A.

    SQLite supports the following data types:

    1. NULL: Indicates that the value is NULL.
    2. INTEGER: Represents an integer, which can store an integer of 1, 2, 3, 4, 6 or 8 bytes.
    3. REAL: Indicates floating-point numbers, which can store 8-byte IEEE floating-point numbers.
    4. TEXT: Indicates a text string, which is stored using encoding.
    5. BLOB: Represents binary data, which can store any type of data, such as images, audio, video, etc.
    6. NUMERIC: An exact numeric type that can store numeric values of arbitrary precision.

    So if you want to use Entity Framework Core to save Dictionary<string, object> to SQLite database, you need to create your own entity class to represent the dictionary structure.

    The following is my example, and after testing the migration is successful, I changed the object to a specific class:

         public class DictionaryEntity
         {
             public int Id { get; set; }
             public string Key { get; set; }
             public Add Value { get; set; }
         }
         public class Add
         {
             public int Id { get; set; }
         }
    
    
    

    Best Regards,

    Wenbin


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.  

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.