Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
694 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Hi,
I am trying to set null to a datetime field but unfortunately it is not setting null.
public async Task UpdateOrderDetailAsync(OrderDetail orderDetail)
{
var detail = await this._db.OrdersDetail.FindAsync(orderDetail.Id);
if (detail != null)
{
detail.Quantity = orderDetail.Quantity;
detail.CostRatio = orderDetail.CostRatio;
detail.Description = orderDetail.Description;
detail.ProductCode = orderDetail.ProductCode;
detail.ProductName = orderDetail.ProductName;
detail.BuyUnitPrice = orderDetail.BuyUnitPrice;
detail.ShippingNumber = orderDetail.ShippingNumber;
detail.Status = orderDetail.Status;
detail.TotalBuyPrice = orderDetail.BuyUnitPrice * orderDetail.Quantity;
detail.TotalSellPrice = orderDetail.Quantity * orderDetail.SellUnitPrice;
detail.SellUnitPrice = orderDetail.SellUnitPrice;
detail.UnitCost = (orderDetail.BuyUnitPrice * (orderDetail.CostRatio / 100)) + orderDetail.BuyUnitPrice;
detail.TotalUnitCost = orderDetail.UnitCost * orderDetail.Quantity;
detail.Currency = orderDetail.Currency;
detail.CustomerOrderNumber = orderDetail.CustomerOrderNumber;
detail.CustomerStockCode = orderDetail.CustomerStockCode;
detail.OrderId = orderDetail.OrderId;
detail.VendorId = orderDetail.VendorId;
detail.TrackingNumber = orderDetail.TrackingNumber;
detail.Warehouse = orderDetail.Warehouse;
detail.PaymentStatus = orderDetail.PaymentStatus;
if (detail.Status == "Completed")
{
detail.CompletionDateTime = DateTime.Now;
}
else
{
orderDetail.CompletionDateTime = null;
}
await _db.SaveChangesAsync();
}
}
Here is entity
public class OrderDetail
{
public int Id { get; set; }
[Required]
[MaxLength(100)]
public string ProductCode { get; set; }
[MaxLength(250)]
public string? ProductName { get; set; }
[Required]
public int Quantity { get; set; }
[Required]
public double BuyUnitPrice { get; set; }
public double CostRatio { get; set; }
public double UnitCost { get; set; }
public double TotalBuyPrice { get; set; }
public double? SellUnitPrice { 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 string? Currency { get; set; }
public string? CustomerStockCode { get; set; }
public string? CustomerOrderNumber { get; set; }
public int IsActive { get; set; }
public double? TotalUnitCost { get; set; }
public int OrderId { get; set; }
public int VendorId { get; set; }
public string? Warehouse { get; set; }
public string? PaymentStatus { get; set; }
public Order Order { get; set; }
public Vendor Vendor { get; set; }
[DataType(DataType.DateTime)]
public DateTime? CompletionDateTime { get; set; }
}
Hi,
Maybe there is an error in the code, you should put detail instead of orderDetail.
Hi,
In Entity Framework Core, you can set a DateTime property to null by assigning it the value null.
Note that this will only work if the DateTime property is nullable in your entity class. You can make a DateTime property nullable by defining it as DateTime? instead of just DateTime.
Thank you :) Late night coding mistakes.