2

I have an Item entity:

public class Item
{
    public long Id { get; set; }
    public string Name { get; set; }
    public string Skuid { get; set; }
    public double Price { get; set; }
    public double Amount { get; set; }
}

And an Order entity:

public class Order
{
    public long Id { get; set; }
    public DateTime Date { get; set; }
    public StatusEnum Status { get; set; }
    public long SellerId { get; set; }
    public Seller Seller { get; set; }
    public double Total { get; set; }
    public IList<Item> Items { get; set; }
}

My objective is to save the Order containing the items in the Database, but when the request is made, only the reference of my entity "Seller" is saved, but not the references of the items.

I did the following mapping on the "Order" entity:

public class OrderMap : IEntityTypeConfiguration<Order>
{
    public void Configure(EntityTypeBuilder<Order> builder)
    {
        builder.ToTable("Orders");

        builder.HasKey(x => x.Id);

        builder.Property(x => x.Id)
            .ValueGeneratedOnAdd()
            .UseIdentityColumn();

        builder.Property(x => x.Date)
            .IsRequired()
            .HasColumnName("DateSale")
            .HasColumnType("DATETIME")
            .HasDefaultValueSql("GETDATE()");
        
        builder.Property(x => x.Total)
            .IsRequired()
            .HasColumnName("Total")
            .HasColumnType("DECIMAL");

        builder.Property(x => x.Status)
            .IsRequired()
            .HasConversion(
                v => v.ToString(),
                v => (StatusEnum)Enum.Parse(typeof(StatusEnum), v))
            .HasColumnName("Status")
            .HasColumnType("NVARCHAR") //verificar se nao vai dar conflito
            .HasMaxLength(120); 

        builder
            .HasOne(x => x.Seller)
            .WithOne()
            .HasConstraintName("FK_Seller_Order")
            .OnDelete(DeleteBehavior.Cascade);

        builder
            .HasMany(x => x.Items)
            .WithOne()
            .HasConstraintName("FK_Item_Order")
            .IsRequired()
            .OnDelete(DeleteBehavior.Cascade); //
    }
}

What is the correct mapping to do?

  • 1
    Can you show the code where you are saving the entities? Your Item class doesn't have a member for a column linking to the Order class, may be an issue. – SBFrancies Dec 23 '22 at 21:38
  • Can't be answered without seeing the actual running code. The class model and the mapping shouldn't be a problem. – Gert Arnold Dec 30 '22 at 16:45

1 Answers1

0

Please first add these properties OrderId and Order to your Item entity class. This way you are instructions the framework to create an OrderId foreign key column in the database.

public class Item
{
   public long Id { get; set; }
   public string Name { get; set; }
   public string Skuid { get; set; }
   public double Price { get; set; }
   public double Amount { get; set; }
   public long OrderId { get; set; }
   public Order Order {get; set; }
}

And perhaps this part

builder.HasMany(x => x.Items).WithOne()

should be like this

 builder.HasMany(x => x.Items).WithOne(x => x.Order)
Anton Kovachev
  • 322
  • 3
  • 6