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();
}
var word = db.Words.Include(x => x.LearnDay).Where(p => p.Id == testId).FirstOrDefault();