1

I have an "Item" table defined as such:

item_id
Name
Description
itemseries_id
itemtype_id
itemcondition_id

And then I have "ItemForSale" table:

itemforsale_id
item_id
price
date_added

Entities:

public class Item
{
    public int ItemId { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    public int ItemSeriesId { get; set; }
    public ItemSeries ItemSeries { get; set; }

    public int ItemConditionId { get; set; }
    public ItemCondition ItemCondition { get; set; }

    public int ItemTypeId { get; set; }
    public ItemType ItemType { get; set; }

    public List<ItemTag> ItemTags { get; set; }
    public List<ItemImage> ItemImages { get; set; }
    public List<ItemPurchase> ItemPurchases { get; set; }
    public List<ItemSale> ItemSales { get; set; }
}
public class ItemForSale
{
    public int ItemForSaleId { get; set; }
    public decimal Price { get; set; }
    public DateTime AddedDate { get; set; }

    public int ItemId { get; set; }
    public Item Item { get; set; }
}

How would I use the FluentAPI between these? I know I could add a reference to ItemForSale inside the Item entity class, but it doesn't make sense to me. So far I have mapped all of my One-to-one and many-to-many relationships, but the relationship between Item and ItemForSale is just confusing me.

Note: I am distinguishing between items that have been sold as a "Sale" or "ItemSale" and an item up for sale with no buyer as "ItemForSale"

alioc
  • 13
  • 3
  • Hey @alioc, Welcome to Stack Overflow! As far as I could understand, you can just map your relationship as if it would be a "one-to-many" relationship, like in this documentation page here: https://learn.microsoft.com/pt-br/ef/core/modeling/relationships?tabs=fluent-api%2Cfluent-api-simple-key%2Csimple-key – cezarlamann Sep 01 '20 at 13:54
  • This can also help you: I don't think it qualifies this question to be a duplicate: https://stackoverflow.com/questions/35562483/equivalent-for-hasoptional-in-entity-framework-core-1-ef7 – cezarlamann Sep 01 '20 at 14:16

1 Answers1

1

From the EF Core Docs, you can do something like this:

class MyContext : DbContext
{
    public DbSet<Item> Items { get; set; }
    public DbSet<ItemSale> ItemSales { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ItemSale>()
            .HasOne(p => p.Item)
            .WithMany(b => b.ItemSales)
            .HasForeignKey(p => p.ItemId)
            .IsRequired(false);
    }
}

public class Item
{
    public int ItemId { get; set; }
    public string Url { get; set; }

    public List<ItemSale> ItemSales { get; set; }
}

public class ItemSale
{
    public int ItemSaleId { get; set; }
    public string Title { get; set; }
    public string Content { get; set; }

    public Item Item { get; set; }
    
    // note that the reference id is nullable
    public int? ItemId { get; set; }
}

And then, mark the property ItemId in your model class as int?. While on EF 6, we had the HasOptional configuration option, but on EF Core, if the reference property can be nullable, it assumes the property starts from 0, like 0..N. I think that even the IsRequired(false) is not needed in this context as well, but here it goes.

cezarlamann
  • 1,465
  • 2
  • 28
  • 43