EF trying to make a record I haven’t instructed it to?

  Kiến thức lập trình

This speaks absolutely volumes to EF’s poor nature of thinking it knows better than the developer. With its magic behind the scenes I’m struggling to decipher how its heading towards failure.

It’s trying to create a record of entity CatalogItem when I make a new PlayerFurnitureItem record, which results in

MySqlConnector.MySqlException (0x80004005): Field ‘stack_limit’
doesn’t have a default value

stack_limit belongs to catalog_items so this was a red flag straight away

If I comment out the for loop it works fine.

var newItems = new List<PlayerFurnitureItem>();
var metaData = NetworkPacketEventHelpers.CalculateMetaDataForCatalogItem(eventParser.ExtraData, item);
var furnitureItem = item.FurnitureItems.First();

for (var i = 0; i < eventParser.Amount; i++)
{
    var newItem = new PlayerFurnitureItem
    {
        PlayerId = client.Player.Id,
        FurnitureItem = furnitureItem,
        LimitedData = $"1:1",
        MetaData = metaData,
        CreatedAt = created
    };
    
    newItems.Add(newItem);
    client.Player.FurnitureItems.Add(newItem);
}

dbContext.PlayerFurnitureItems.AddRange(newItems);
await dbContext.SaveChangesAsync();

LEAVE A COMMENT