EF Core 6 - DateTime how to set null

asked 2023-01-02T17:50:14.69+00:00
Cenk 666 Reputation points

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; }  
  
    }  
Entity Framework Core
Entity Framework Core
A lightweight, extensible, open-source, and cross-platform version of the Entity Framework data access technology.
420 questions
C#
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.
6,922 questions
No comments
1 vote

Accepted answer
  1. answered 2023-01-02T18:05:36.02+00:00
    Arharbi, Adnane 131 Reputation points

    Hi,

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

    275511-image.png

    No comments

2 additional answers

Sort by: Most helpful
  1. answered 2023-01-02T18:25:12.08+00:00
    Cenk 666 Reputation points

    Thank you :) Late night coding mistakes.


  2. answered 2023-01-02T18:01:48.187+00:00
    Arharbi, Adnane 131 Reputation points

    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.

    No comments