M
M
mr_blond972018-10-20 01:11:14
ORM
mr_blond97, 2018-10-20 01:11:14

Object with fields (one-to-many) returned with null in fields in Entity Framework, how to solve this?

I am using Entity Framework and SQLite.

Context:
public class WordContext : DbContext
{
    public DbSet<VmWord> Words { get; set; }
    public DbSet<LearnDay> LearnDay { get; set; }

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

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}

Models:
public class Word
{
    public int Id { get; set; }
    public IList<LearnDay> LearnDay { get; set; }
}

public class LearnDay
{
    public int Id { get; set; }
    public string Key { get; set; }
    public int Value { get; set; }
    public Word Word { get; set; }
}

Object initialization:
using (var db = new WordContext())
{
    db.Words.Add(new VmWord
    {
        LearnDay = new List<LearnDay> 
                    {
                        new LearnDay {
                            Key = "pl",
                            Value = 0
                        },
                        new LearnDay {
                            Key = "en",
                            Value = 0
                        }
                    }
    });
    db.SaveChanges();
}


At this step, everything works, the values ​​​​are saved in the tables.
But then I get the object:
using (var db = new WordContext())
{
    // word != null и LearnDay == null
    var word = db.Words.Where(p => p.Id == testId).FirstOrDefault();
}


Field LearnDay == null .
But if you do it like this:
I use Entity Framework and SQLite.

The context:

public class WordContext : DbContext
{
    public DbSet<VmWord> Words { get; set; }
    public DbSet<LearnDay> LearnDay { get; set; }

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

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
    }
}
The models:

public class Word
{
    public int Id { get; set; }
    public IList<LearnDay> LearnDay { get; set; }
}

public class LearnDay
{
    public int Id { get; set; }
    public string Key { get; set; }
    public int Value { get; set; }
    public Word Word { get; set; }
}
How I initialize and save object:

using (var db = new WordContext())
{
    db.Words.Add(new VmWord
    {
        LearnDay = new List<LearnDay> 
                    {
                        new LearnDay {
                            Key = "pl",
                            Value = 0
                        },
                        new LearnDay {
                            Key = "en",
                            Value = 0
                        }
                    }
    });
    db.SaveChanges();
}
At this steep it looks like it work ok, I open the db and check that tables contain this data.

But then I try to get object and it returns with:

LearnDay == null
using (var db = new WordContext())
{
    // word != null but LearnDay == null
    var word = db.Words.Where(p => p.Id == testId).FirstOrDefault();
}
And if I do:

using (var db = new WordContext())
{
    // learnDay != null
    // learnDay.Word !=null
    // even learnDay.Word.learnDay !=null
    var learnDay = db.LearnDay.Where(p => p.Word == testWord).FirstOrDefault();

    // then try again to get word:
    // word != null
    // LearnDay != null
    var word = db.Words.Where(p => p.Id == testId).FirstOrDefault();
}


And after this step, the field Word.LearnDay != null .

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
eRKa, 2018-10-20
@mr_blond97

See the EF documentation for the Include() method, why it is needed and what it does.
If you need the dependents to be pulled up when querying Word, in addition to the data of the entity itself, you need to do this

var word = db.Words.Include(x => x.LearnDay).Where(p => p.Id == testId).FirstOrDefault();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question