I have a model PlayerContext
which has one entity of type Inventory
(which is a derived type of ItemContainer
). These are all within my DbContext
called ChunkContext
. When I create a new PlayerContext
a Inventory
is also constructed and both are saved in the database with the correct ID's and everything (I can confirm by looking in the DB). For some reason when I load the PlayerContext
later (after it's been saved) the Inventory
is not being loaded (it has the default value and ID of 0 which is incorrect).
Here is how I'm loading the player (and adding to them to the database if they are a new player):
using (var db = new ChunkContext())
{
PlayerContext playerContext = db.Players
.Where(p => p.username == username)
.Include(p => p.inventory)
.FirstOrDefault();
// this means the player couldn't be grabbed from the DB
if (playerContext == null)
{
playerContext = new PlayerContext();
playerContext.username = username;
db.Players.Add(playerContext);
db.SaveChanges();
}
return new Player(peer, playerContext);
}
And here is all of the model classes:
class ChunkContext : DbContext
{
...
public DbSet<PlayerContext> Players { get; set; }
...
public DbSet<ItemContainerContext> ItemContainers { get; set; }
public DbSet<Inventory> Inventories { get; set; }
...
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
...
modelBuilder.Entity<PlayerContext>().HasOne(p => p.inventory);
...
modelBuilder.Entity<ItemContainerContext>().HasKey(ic => ic.id);
...
base.OnModelCreating(modelBuilder);
}
_
[Index(nameof(username), IsUnique = true)]
public class PlayerContext
{
public PlayerContext()
{
inventory = new Inventory();
}
...
[Key]
public int id { get; set; }
...
[Required]
public Inventory inventory { get; set; }
...
}
_
public class Inventory : ItemContainerContext
{
... unrelated gameplay code ...
}
_
public class ItemContainerContext
{
[Key]
public int id { get; set; }
public ItemContainerContext()
{
...
}
...
}