Answer the question
In order to leave comments, you need to log in
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.
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);
}
}
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; }
}
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();
}
using (var db = new WordContext())
{
// word != null и LearnDay == null
var word = db.Words.Where(p => p.Id == testId).FirstOrDefault();
}
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();
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question