0

I'm trying to learn relational databases using SQLite and Entity Framework, I made a database.db using Db Browser for SQLite with 2 tables: albums and songs:

albums table description songs table description

here's the DbContext to manage them.: 

namespace SQLite_test2
{
    public class DatabaseContext : DbContext
    {
        public DbSet<album> albums { get; set; }
        public DbSet<song> songs { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder dbContextOptionsBuilder)
        {
            dbContextOptionsBuilder.UseSqlite("Data Source =database.db");
        }
    }


    public class album
    {
        public int id { get; set; }
        public string title { get; set; }
        public string artist { get; set; }
        public List<song> songs { get; set; }

    }
    public class song
    {
        public int id { get; set; }
        public string songname { get; set; }

        public int albumId { get; set; }

        public album album { get; set; }
    }

}

Now I try to make the example entries for both tables and make a relationship:

class Program
{
    static void Main(string[] args)
    {

        using ( var database = new DatabaseContext() )
        {
            //create entries. Entries are added succesfully if yo do it without relationship
            database.albums.Add(new album() { artist = "Slayer", title = "Reign in Blood" });
            database.songs.Add(new song() { songname = "Angel of Death" });

            //make a relationship
            var myAlbum = database.albums.Find(1);
            var mySong = database.songs.Find(1);
            //System.NullReferenceException: "Object 
            //reference not set to an instance of an 
            //object."
            //SQLite_test2.album.songs.get returned null.
            myAlbum.songs.Add(mySong);

            database.SaveChanges();
        }
    }
}

EDIT: As suggested by @Andre.Santarosa I edited the code by adding database.SaveChanges(); after making table entries. It adds entries to the database and gives SQLite_test2.album.songs.get returned null. exception:

class Program
{
    static void Main(string[] args)
    {

        using ( var database = new DatabaseContext() )
        {
            database.albums.Add(new album() { artist = "Slayer", title = "Reign in Blood" });
            database.songs.Add(new song() { songname = "Angel of Death" });
            database.SaveChanges();
            var myAlbum = database.albums.Find(1);
            var mySong = database.songs.Find(1);
            myAlbum.songs.Add(mySong);
            database.SaveChanges();
            //var mishaAlbum = database.albums.Find(4);
            //var mishaSong = database.songs.Find(1);
            //mishaAlbum.songs.Add(mishaSong);

        }
    }
}
Igor Cheglakov
  • 525
  • 5
  • 11
  • First you need to `SaveChanges()` after this you'll be able to query the data with `.Find(1)` – Andre.Santarosa Jan 17 '21 at 13:04
  • This gives me `SQLite_test2.album.songs.get returned null.` even though I can see an entry with index `1` in the browser. – Igor Cheglakov Jan 17 '21 at 13:06
  • Be aware that relationships created on the database doesn't reflect on you code, you need to explicit declare them – Andre.Santarosa Jan 17 '21 at 13:07
  • You mean that the data is recorded in your database but the code doesn't retrieve this info, right? Please update the post with the updated code – Andre.Santarosa Jan 17 '21 at 13:08
  • 1
    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) – Charlieface Jan 17 '21 at 13:17

0 Answers0