C
C#6mo ago
PIE

✅ List<KeyValuePair<Trader, long>> requires primary key to be defined

This is my main model i'm trying to pass
public class Item
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public List<KeyValuePair<Trader, long>> TraderPrices { get; set; }
}
public class Item
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public List<KeyValuePair<Trader, long>> TraderPrices { get; set; }
}
with Trader as
[Keyless]
public class Trader
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsTeTrader { get; set; }
public bool IsAwTrader { get; set; }
}
[Keyless]
public class Trader
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsTeTrader { get; set; }
public bool IsAwTrader { get; set; }
}
with a basic database
class ItemDb : DbContext
{
public ItemDb(DbContextOptions<ItemDb> options)
: base(options) { }

public DbSet<Item> Items => Set<Item>();
}
class ItemDb : DbContext
{
public ItemDb(DbContextOptions<ItemDb> options)
: base(options) { }

public DbSet<Item> Items => Set<Item>();
}
for some reason, passing a Item class through this this method
db.Items.Add(itemInput);
await db.SaveChangesAsync();
return Results.Created($"/items/{itemInput.Id}", itemInput);
db.Items.Add(itemInput);
await db.SaveChangesAsync();
return Results.Created($"/items/{itemInput.Id}", itemInput);
throws the error in the title is there a way to set List<KeyValuePair<Trader, long>> to be keyless?
6 Replies
Angius
Angius6mo ago
How would you expect a List<KeyValuePair<Trader, long>> to be represented in the database? Also, what database are you using?
PIE
PIEOP6mo ago
EntityFrameworkCore.InMemory
Angius
Angius6mo ago
Right, so it probably does not support JSON columns
PIE
PIEOP6mo ago
ah... would there be an alternative way to store something like this than?
{
"Id": 123,
"Name": "123",
"TraderPrices": [
{
"Key": {
"Id": 123,
"Name": "123",
"IsTeTrader": false,
"IsAwTrader": false
},
"Value": 123
}
]
}
{
"Id": 123,
"Name": "123",
"TraderPrices": [
{
"Key": {
"Id": 123,
"Name": "123",
"IsTeTrader": false,
"IsAwTrader": false
},
"Value": 123
}
]
}
with proper order? or would I need to use something else?
Angius
Angius6mo ago
I'd rethink your schema completely It's a relational database, so use that fact No need for the whole trader to be a key anywhere An ID is enough I'd have a Trader, an Item, and a TraderPrice TraderPrice referencing both item and trader Something like
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public List<TraderPrice> TraderPrices { get; set; }
}

public class Trader
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsTeTrader { get; set; }
public bool IsAwTrader { get; set; }
public List<TraderPrice> TraderPrices { get; set; }
}

public class TraderPrice
{
public Item Item { get; set; }
public int ItemId { get; set; }
public Trader Trader { get; set; }
public int TraderId { get; set; }
public decimal Price { get; set; }
}
public class Item
{
public int Id { get; set; }
public string Name { get; set; }
public List<TraderPrice> TraderPrices { get; set; }
}

public class Trader
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsTeTrader { get; set; }
public bool IsAwTrader { get; set; }
public List<TraderPrice> TraderPrices { get; set; }
}

public class TraderPrice
{
public Item Item { get; set; }
public int ItemId { get; set; }
public Trader Trader { get; set; }
public int TraderId { get; set; }
public decimal Price { get; set; }
}
Something like that
PIE
PIEOP6mo ago
ok thank you o/
Want results from more Discord servers?
Add your server