EF Core: Looking for non existent column?

EDIT: Issue doesn’t exist if I remove RelatedItems property.

Upon creating a record of entity type PlayerFurnitureItemPlacementData, it tries to insert using a non existing column.

Signature generated:

INSERT INTO `player_furniture_item_placement_data` (`created_at`, `direction`, `player_furniture_item_id`, `player_furniture_item_wired_data_id`, `position_x`, `position_y`, `position_z`, `room_id`, `wall_position`)

player_furniture_item_wired_data_id has never existed, where has EF Core got this from?

Full code:

new PlayerFurnitureItemPlacementData
{
    RoomId = room.Id,
    PlayerFurnitureItemId = playerItem.Id,
    PlayerFurnitureItem = playerItem,
    PositionX = x,
    PositionY = y,
    PositionZ = z,
    WallPosition = string.Empty,
    Direction = (HDirection) direction,
    CreatedAt = DateTime.Now
};

dbContext.Entry(roomFurnitureItem.PlayerFurnitureItem).State = EntityState.Unchanged;
dbContext.RoomFurnitureItems.Add(roomFurnitureItem);

Entities (truncated if unrelated):

public class PlayerFurnitureItemPlacementData
{
    [Key] 
    public int Id { get; init; }

    public int PlayerFurnitureItemId { get; init; }
    public required PlayerFurnitureItem PlayerFurnitureItem { get; init; }
    public int RoomId { get; init; }
    public Room? Room { get; init; }
}

public class PlayerFurnitureItem
{
    public int Id { get; init; }
    public int PlayerId { get; set; }
    public Player? Player { get; set; }
    public FurnitureItem? FurnitureItem { get; init; }
    public PlayerFurnitureItemPlacementData? PlacementData { get; set; }
    public PlayerFurnitureItemWiredData? WiredData { get; set; }
    public DateTime CreatedAt { get; init; }
}

public class PlayerFurnitureItemWiredData
{
    [Key] 
    public int Id { get; init; }

    public int PlayerFurnitureItemId { get; init; }
    public required PlayerFurnitureItem PlayerFurnitureItem { get; init; }
    public required ICollection<PlayerFurnitureItemPlacementData> RelatedItems { get; init; }
}

When you introduced:

public required ICollection<PlayerFurnitureItemPlacementData> RelatedItems

on the Player Furniture Item Wired Data, EF is looking for a way to wire that up. It either forms a many-to-one or a many-to-many relationship which you have not configured, so EF assumes a many-to-one. For one of these related items to be associated to the wired data, it needs a FK to the wired data so EF created one. If there is a different column on the table that EF should use you can either create the FK and associate it, as per Esanju’s answer, or if you just want the association to use the column without the FK property exposed or a navigation property on the other end, you can use a shadow property for the FK:

modelBuilder.Entity<PlayerFurnitureItemWiredData>()
    .HasMany(x => x.RelatedItems)
    .WithOne() // no navigation property on other side.
    .HasForeignKey("PlayerFurnitureItemWiredDataId"); // Insert whichever FK which points back to the wired data.

This tells EF which column to use on the table for the FK without exposing it or the other side of the relationship in the entity.

The Entity Framework (EF) automatically constructs foreign keys and joins from the navigation properties and their related entities so with explicitly definition, it should solve the issue.

PlayerFurnitureItemPlacementData class

public int? PlayerFurnitureItemWiredDataId { get; set; }
public PlayerFurnitureItemWiredData? PlayerFurnitureItemWiredData { get; set; }

Define the relationship explicitly on the OnModelBinding Method

modelBuilder.Entity<PlayerFurnitureItemPlacementData>()
    .HasOne(p => p.PlayerFurnitureItemWiredData)
    .WithMany(w => w.RelatedItems)
    .HasForeignKey(p => p.PlayerFurnitureItemWiredDataId);

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật