C#C
C#3y 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; }


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;


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);
}


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?
Was this page helpful?