C
C#6mo ago
DapperDeer

EntityFramework Property Binding

I have class Pokemon:
[JsonPropertyName("Name")]
public string Name { get; set; }

[Key]
[JsonPropertyName("Pokedex Number")]
public int PokedexNumber { get; set; }

[ForeignKey(nameof(PokeType.Id))]
[JsonPropertyName("Type")]
public PokeType Type { get; set; }
[JsonPropertyName("Name")]
public string Name { get; set; }

[Key]
[JsonPropertyName("Pokedex Number")]
public int PokedexNumber { get; set; }

[ForeignKey(nameof(PokeType.Id))]
[JsonPropertyName("Type")]
public PokeType Type { get; set; }
Where PokeType is:
[Key]
public int Id { get; set; }
[JsonPropertyName("TypeSlotOne")]
public Types SlotOne { get; set; } = Types.None;
[JsonPropertyName("TypeSlotTwo")]
public Types SlotTwo { get; set; } = Types.None;
[Key]
public int Id { get; set; }
[JsonPropertyName("TypeSlotOne")]
public Types SlotOne { get; set; } = Types.None;
[JsonPropertyName("TypeSlotTwo")]
public Types SlotTwo { get; set; } = Types.None;
and I am getting a list of Pokemon from an external API, converting them to my own classes, and storing them in a Sqlite db where my DbContext is as follows:
public DbSet<Pokemon> Pokemon { get; set; }
public DbSet<PokeType> PokeTypes { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PokeType>()
.Property(e => e.SlotOne)
.HasConversion(
e => e.ToString(),
e => (Types)Enum.Parse(typeof(Types), e));

modelBuilder.Entity<PokeType>()
.Property(e => e.SlotTwo)
.HasConversion(
e => e.ToString(),
e => (Types)Enum.Parse(typeof(Types), e));

base.OnModelCreating(modelBuilder);
}
public DbSet<Pokemon> Pokemon { get; set; }
public DbSet<PokeType> PokeTypes { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PokeType>()
.Property(e => e.SlotOne)
.HasConversion(
e => e.ToString(),
e => (Types)Enum.Parse(typeof(Types), e));

modelBuilder.Entity<PokeType>()
.Property(e => e.SlotTwo)
.HasConversion(
e => e.ToString(),
e => (Types)Enum.Parse(typeof(Types), e));

base.OnModelCreating(modelBuilder);
}
When I am accessing the cached data, my Pokemon aren't filling in their Types and I don't know how to map the properties. EF has also made Pokemon and PokeType into two separate tables, which doesn't necessarily seem like a major issue, I just don't know how to workaround it. Some StackOverflow answers I've seen that may help have a .Map method that I don't seem to have - maybe I'm missing a dependency?
3 Replies
DapperDeer
DapperDeer6mo ago
I have a DbContextFactory that I am using to get a new DbContext and assigning the calling class's ObservableCollection<Pokemon> prop to DbContext.Set<Pokemon>(), so it could also be how I'm calling and getting the values, but I think it's more to do with how I'm setting up the model creation.
Angius
Angius6mo ago
You need to .Include() the properties you want to load Or better yet, .Select() that data into DTOs I'd also store the types in its own table and map them with many-to-many, but that's besides the point
DapperDeer
DapperDeer6mo ago
Is that on the DbContext or in the OnModelCreating? On the DbContext! Thanks, Z :)