EF Core 6 - DateTime how to set null

Cenk 1,041 Reputation points
2023-01-02T17:50:14.69+00:00

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; }  
  
    }  
Developer technologies | .NET | Entity Framework Core
Developer technologies | C#
Developer technologies | C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
0 comments No comments
{count} vote

Answer accepted by question author
  1. Arharbi, Adnane 136 Reputation points
    2023-01-02T18:05:36.02+00:00

    Hi,

    Maybe there is an error in the code, you should put detail instead of orderDetail.

    275511-image.png

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Arharbi, Adnane 136 Reputation points
    2023-01-02T18:01:48.187+00:00

    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.

    1 person found this answer helpful.
    0 comments No comments

  2. Cenk 1,041 Reputation points
    2023-01-02T18:25:12.08+00:00

    Thank you :) Late night coding mistakes.

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.