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
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
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!