Hi guys,
I am working on an ASP.NET Core Blazor Server app. I have one to many relationships between Order and OrderDetail entities. I couldn't find a way to insert data into tables from excel upload. Any help would be great.
Thanks in advance.
Here are the entities:
C#
public class Order
{
public int Id { get; set; }
[Required]
public DateTime OrderDateTime { get; set; }
[Required]
[MaxLength(250)]
public string CustomerName { get; set; }
[Required]
[MaxLength(250)]
public string VendorName { get; set; }
public string Status { get; set; }
[MaxLength(50)]
public string DoneBy { get; set; }
public List OrderDetails { get; set; }
}
public class OrderDetail
{
public int Id { get; set; }
[Required]
[MaxLength(100)]
public string ProductCode { get; set; }
[Required]
[MaxLength(250)]
public string ProductName { get; set; }
[Required]
public int BuyQuantity { get; set; }
[Required]
public int SellQuantity { get; set; }
public double CostRatio { get; set; }
public double UnitCost { get; set; }
public double TotalBuyPrice { get; set; }
public double TotalSellPrice { get; set; }
[MaxLength(150)]
public string ShippingNumber { get; set; }
public string Status { get; set; }
[MaxLength(150)]
public string TrackingNumber { get; set; }
[MaxLength(400)]
public string Description { get; set; }
public int OrderId { get; set; }
public virtual Order Order { get; set; }
}Here is the sample excel:


Jignesh KumarRank #91 · 22.9K reputationPosted Jul 14, 2022, 11:38 PM EDT
Jaydeep PatilRank #205 · 11.7K reputationPosted Jul 16, 2022, 2:01 AM EDT
Check the following link that might help you to import EXCEL data into the SQL Tables through C# Code
https://www.c-sharpcorner.com/article/import-excel-data-into-sql-table-using-sqlbulkcopy-in-c-sharp/
https://www.c-sharpcorner.com/UploadFile/0c1bb2/inserting-excel-file-records-into-sql-server-database-using/
Regards,
Jaydeep Patil
RaysefoRank #4,173 · 360 reputationPosted Jul 20, 2022, 2:00 PM EDT