I’ve 3 classes.
- Header
- Property
- Units
Header class has one to many relation with Property Class & Property class has one to many relation with Units class.
Header DTO:
public class HeaderDTO
{
public string Phone { get; set; } = null!;
public string? Source { get; set; }
public string? Address { get; set; }
public string? Country { get; set; }
public string? ShipTo { get; set; }
public virtual ICollection<PropertyDTO> Properties { get; set; } = new List<PropertyDTO>();
}
Property DTO:
public class PropertyDTO
{
public string? Floor { get; set; }
public string? Area { get; set; }
public int Window { get; set; }
public string Products { get; set; } = null!;
public string? Login { get; set; }
public string? ShipTo { get; set; }
public int? Flag { get; set; }
public virtual ICollection<UnitDTO> Units { get; set; } = new List<UnitDTO>();
}
Units DTO:
public class UnitDTO
{
public string? UniqueName { get; set; }
public int Height { get; set; }
public int Width { get; set; }
public int Panel { get; set; }
public int? Belah { get; set; }
public string? Img1 { get; set; }
public string? Img2 { get; set; }
public string? Img3 { get; set; }
public List<TDataProduct>? products;
}
I need to post multiple JSON records of Property & Units.
How can I iterate to units class.
I’ve tried to Call Include function but It’s not valid in this context.
Any guidance to solve this problem will be very much appreciated. 🙂
[HttpPost("SOPPOST")]
public async Task<ActionResult<TDataHeader>> CreateHeaders(HeaderDTO hdto)
{
var header = new HeaderDTO
{
Address = hdto.Address,
Country = hdto.Country,
Phone = hdto.Phone,
ShipTo = hdto.ShipTo,
Source = hdto.Source
};
var phoneVerify = _context.TDataHeaders.Count(x => x.Phone == header.Phone);
var headerImport = new TDataHeader
{
Address = header.Address,
Country = header.Country,
Phone = header.Phone,
ShipTo = header.ShipTo,
Source = header.Source
};
//if (phoneVerify == 0)
//{
// await _context.TDataHeaders.AddAsync(headerImport);
//}
foreach (var prop in hdto.Properties)
{
var property = new TDataProperty
{
Floor = prop.Floor,
Area = prop.Area,
Windows = prop.Windows,
Flag = prop.Flag,
Login = prop.Login,
Products = prop.Products,
ShipTo = prop.ShipTo,
Header = headerImport
};
foreach (var unit in prop.Units)
{
var unitModel = new TDataUnit
{
Belah = unit.Belah,
Height = unit.Height,
Width = unit.Width,
Panel = unit.Panel,
Img1 = unit.Img1,
Img2 = unit.Img2,
Img3 = unit.Img3,
UniqueName = prop.Floor + "-" + prop.Area + "-" + prop.Windows,
Property = property
};
await _context.TDataUnits.AddRangeAsync(unitModel);
}
await _context.TDataProperties.AddRangeAsync(property);
}
await _context.SaveChangesAsync();
return Ok("Posted");
}
Above is working fine but need the Lambda Expression for the same.
3