简体   繁体   中英

Entity Framework SQLite can't retrieve related object

I'm trying to recreate the sticky notes app in windows 10 using WPF. I am using Entity Framework with an SQLite database. And EF can't retrieve an object which is the child of another. How can I do that?

I have basically 2 classes i wanna store in this database StickyNote and SitckyNoteGroup. Each StickyNote has a StickyNoteGroup. In order you to fully understand I'll post all the classes involved in my problem but here is the github repo if you want https://github.com/Kamigoro/StickyNotes .

namespace Todo.Models
{
    public class StickyNote
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Text { get; set; }
        public StickyNoteGroup Group { get; set; }

        public override string ToString()
        {
            return $"Name : {Name}\nText : {Text}";
        }

    }
}
namespace Todo.Models
{
    public class StickyNoteGroup
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Color { get; set; }

        public override string ToString()
        {
            return $"Name : {Name}\nColor : {Color}";
        }
    }
}

My class using EntityFramework looks like this and is called NoteContext

using Microsoft.EntityFrameworkCore;

namespace Todo.Models.DataAccess
{
    public class NoteContext : DbContext
    {
        public DbSet<StickyNote> Notes { get; set; }
        public DbSet<StickyNoteGroup> NoteGroups { get; set; }


        protected override void OnConfiguring(DbContextOptionsBuilder options)
            => options.UseSqlite("Data Source=Todo.db");

    }
}

And finally the class that I use to do CRUD operations is called DataAccessor and looks like this.

namespace Todo.Models.DataAccess
{
    public static class DataAccessor
    {
        public static List<StickyNote> GetStickyNotes()
        {
            List<StickyNote> notes;
            using (var database = new NoteContext())
            {
                notes = database.Notes.ToList();
            }
            return notes;
        }

        public static void SaveStickyNote(StickyNote stickyNote)
        {
            using (var database = new NoteContext())
            {
                database.Notes.Add(stickyNote);
                database.SaveChanges();
            }
        }

        public static List<StickyNoteGroup> GetStickyNoteGroups()
        {
            List<StickyNoteGroup> noteGroups;
            using (var database = new NoteContext())
            {
                noteGroups = database.NoteGroups.ToList();
            }
            return noteGroups;
        }

        public static void SaveStickyNoteGroup(StickyNoteGroup stickyNoteGroup)
        {
            using (var database = new NoteContext())
            {
                database.NoteGroups.Add(stickyNoteGroup);
                database.SaveChanges();
            }
        }

    }
}

My question is why this part of code tell me that there is no StickyNoteGroup for the current StickyNote, even though there is one in the sqlite database?

        private void btnAddStickyNote_Click(object sender, RoutedEventArgs e)
        {
            StickyNoteGroup group = new StickyNoteGroup() { Name = "Test Group", Color = "Red" };
            StickyNote note = new StickyNote() { Name = "Note 1", Text = "An attempt to write a note", Group = group };
            DataAccessor.SaveStickyNote(note);

            foreach (StickyNote currentNote in DataAccessor.GetStickyNotes())
            {
                Debug.WriteLine(currentNote.Group.ToString());
            }
        }

StickyNote Table:
便签表

StickyNoteGroup Table:
便笺组表

Thanks a lot for your answers. And if you have other comments to do on my code, they are welcomed, because i don't really know the good patterns to work with an ORM like that.

It seems that you need to add Include call:

 public static List<StickyNote> GetStickyNotes()
    {
        List<StickyNote> notes;
        using (var database = new NoteContext())
        {
            notes = database.Notes
                .Include(n => n.Group)
                .ToList();
        }
        return notes;
    }

BTW you can return evaluated query result data (for example via ToList , ToArray , First and so on) from inside using statement:

 public static List<StickyNote> GetStickyNotes()
    {
        using (var database = new NoteContext())
        {
            return database.Notes
                .Include(n => n.Group)
                .ToList();
        }
    }

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