简体   繁体   English

实体框架 SQLite 无法检索相关 object

[英]Entity Framework SQLite can't retrieve related object

I'm trying to recreate the sticky notes app in windows 10 using WPF.我正在尝试使用 WPF 在 windows 10 中重新创建便签应用程序。 I am using Entity Framework with an SQLite database.我正在使用带有 SQLite 数据库的实体框架。 And EF can't retrieve an object which is the child of another.并且 EF 无法检索 object 这是另一个孩子的孩子。 How can I do that?我怎样才能做到这一点?

I have basically 2 classes i wanna store in this database StickyNote and SitckyNoteGroup.我基本上有 2 个类我想存储在这个数据库 StickyNote 和 SitckyNoteGroup 中。 Each StickyNote has a StickyNoteGroup.每个 StickyNote 都有一个 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 .为了让您充分理解,我将发布与我的问题相关的所有类,但如果您想要https://github.com/Kamigoro/StickyNotes ,这里是 github 存储库。

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我使用 EntityFramework 的 class 看起来像这样,被称为 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.最后,我用来执行 CRUD 操作的 class 称为 DataAccessor,看起来像这样。

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?我的问题是为什么这部分代码告诉我当前 StickyNote 没有 StickyNoteGroup,即使 sqlite 数据库中有一个?

        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.如果您对我的代码有其他意见,欢迎提出,因为我真的不知道使用这样的 ORM 的良好模式。

It seems that you need to add Include call:看来您需要添加Include调用:

 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:顺便说一句,您可以从using语句内部返回评估的查询结果数据(例如通过ToListToArrayFirst等):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM