0

I am developing an app that is going to use a SQLite database in Visual Studio however at the initialization of the program I cannot read / write the database using a DbContext.

I have tried few things from here https://ef.readthedocs.io/en/staging/platforms/netcore/new-db-sqlite.html

and here https://docs.microsoft.com/en-us/ef/core/get-started/?tabs=netcore-cli&fbclid=IwAR0O_1VYLJdOCm2PBf6_W42fNqTE5DpwXXghUNyvf7_92ozKQjCJcS1S6J0

This is my code

using Microsoft.EntityFrameworkCore;

namespace HomeHelper.Core
{
    public class HomeHelperContext : DbContext
    {
        public string Config { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlite("Data Source= Db/HomeHelper.db");
        }
    }
}

This is the database

enter image description here

When I attempt to read it with the following code

        using (var db = new HomeHelperContext())
        {
            if(db!=null)
            {
                if (db.Config == null)
                {
                    db.Config = "test";
                }

                foreach (var Config in db.Config)
                {
                    if (Config != null)
                        Console.WriteLine(" - {0}", Config);
                }
                db.SaveChanges();
            }

        }

I get this error -

System.NullReferenceException: 'Object reference not set to an instance of an object.'
was null.

Can someone tell me what my mistake is, please?

UPDATE: When i manually set the value of Config i can confirm that is has been saved in the database, my question is why i cannot initially read it? If i remove " db.Config = "test"; " i receive null value even knowing the value is set in the database.

Kind regards!

spstrademark
  • 145
  • 1
  • 3
  • 15
  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – madreflection May 14 '20 at 05:34
  • Hello, maybe i did not explain the error clear enough for what i apologize, when attempting to read from the sqlite database with the provided example i get a null value, however if i try to set and save a value i can see if it stored in the database. My question is why i cannot read the value from the sqlite ? Thank you for your reply ! – spstrademark May 14 '20 at 10:02
  • @Houssem Dbira Hello, yes i updated the answer. every time the program starts db.Config is null, i set it with a value " test " and save the changes. And i can see in the database that the value " test " is actually saved. However every time i start the program the initial value of db.Config is null. Thank you for your reply ! Best regards. – spstrademark May 14 '20 at 10:32

1 Answers1

1

As the picture that you shared with us the table name is Settings and Config is just a column name.

The DbContext class must include the DbSet type properties for the entities which map to database tables and views.

    public DbSet<Settings> Settings { get; set; }

and declare your Settings entity model

    public class Settings
    {
        [Key]
        public int Id { get; set; }
        public string Config{ get; set; }

    }

after that you can retrive all data inside Settings with db.Settings

and if you want to retrive the first db.Settings.FirstOrDefault();

https://docs.microsoft.com/en-us/ef/core/get-started/?tabs=netcore-cli

spstrademark
  • 145
  • 1
  • 3
  • 15
H D
  • 625
  • 5
  • 12
  • Hello @Houssem Dbira, i added a [Key] property because without it i had the error " Error: the entity type requires a primary key " I can confirm that is works as intended now thank you kind sir ! Best regards – spstrademark May 14 '20 at 18:58