title: get Discount in DDD With EF Core 6
body: <p>Everything is fine when I save a discount. My codes for saving a discount are as follows.</p>
  <pre><code> #region Static Member(s)
      public static Result&lt;Discount&gt; Create(
          int? minimumPurchase,
          int? minimumDiscountPercent,
          DateTime? minimumvalidDateTo,
          DateTime? minimumvalidDateFrom,
  
          int? discountPercent,
          DateTime? validDateTo,
          DateTime? validDateFrom,
          int? discountType,
          int? vitrinType,
          bool isActive,
          ProductVariant productVariant
          )
      {
          var result = new Result&lt;Discount&gt;();
  
          var minimumPurchaseResult = MinimumPurchase.Create(value: minimumPurchase);
          result.WithErrors(errors: minimumPurchaseResult.Errors);
  
          var minimumDiscountPercentResult = MinimumDiscountPercent.Create(value: minimumDiscountPercent);
          result.WithErrors(errors: minimumDiscountPercentResult.Errors);
  
          var minimumValidDateToResult = MinimumValidDateTo.Create(value: minimumvalidDateTo);
          result.WithErrors(errors: minimumValidDateToResult.Errors);
  
          var minimumValidDateFromResult = MinimumValidDateFrom.Create(value: minimumvalidDateFrom);
          result.WithErrors(errors: minimumValidDateFromResult.Errors);
  
  
          var discountPercentResult = DiscountPercent.Create(value: discountPercent);
          result.WithErrors(errors: discountPercentResult.Errors);
  
          var validDateToResult = ValidDateTo.Create(value: validDateTo);
          result.WithErrors(errors: validDateToResult.Errors);
  
          var validDateFromResult = ValidDateFrom.Create(value: validDateFrom);
          result.WithErrors(errors: validDateFromResult.Errors);
  
          var discountTypeResult = DiscountType.GetByValue(value: discountType);
          result.WithErrors(errors: discountTypeResult.Errors);
  
          var vitrinTypeResult = VitrinType.GetByValue(value: vitrinType);
          result.WithErrors(errors: vitrinTypeResult.Errors);
  
          if (productVariant is null)
          {
              string errorMessage = string.Format(Validations.Required, DataDictionary.ProductVariant);
  
              result.WithError(errorMessage: errorMessage);
          }
  
  
          if (result.IsFailed)
          {
              return result;
          }
  
          var returnValue = new Discount(
              minimumPurchase: minimumPurchaseResult.Value,
              minimumDiscountPercent: minimumDiscountPercentResult.Value,
              minimumValidDateTo: minimumValidDateToResult.Value,
              minimumValidDateFrom: minimumValidDateFromResult.Value,
              discountPercent: discountPercentResult.Value,
              validDateTo: validDateToResult.Value,
              validDateFrom: validDateFromResult.Value,
              discountType: discountTypeResult.Value,
              vitrinType: vitrinTypeResult.Value,
              isActive: isActive,
              productVariant: productVariant
              );
  
          result.WithValue(value: returnValue);
  
          return result;
      }
      #endregion /Static Member(s)
  
      private Discount() : base()
      {
  
      }
  
      private Discount(
          MinimumPurchase minimumPurchase,
          MinimumDiscountPercent minimumDiscountPercent,
          MinimumValidDateTo minimumValidDateTo,
          MinimumValidDateFrom minimumValidDateFrom,
          DiscountPercent discountPercent,
          ValidDateTo validDateTo,
          ValidDateFrom validDateFrom, bool isActive, DiscountType discountType, VitrinType vitrinType, ProductVariant productVariant)
      {
          MinimumPurchase = minimumPurchase;
          MinimumDiscountPercent = minimumDiscountPercent;
          MinimumValidDateTo = minimumValidDateTo;
          MinimumValidDateFrom = minimumValidDateFrom;
          DiscountPercent = discountPercent;
          ValidDateTo = validDateTo;
          ValidDateFrom = validDateFrom;
          IsActive = isActive;
          DiscountType = discountType;
          VitrinType = vitrinType;
          ProductVariant = productVariant;
      }
  
      public MinimumPurchase MinimumPurchase { get; private set; }
      public MinimumDiscountPercent MinimumDiscountPercent { get; private set; }
      public MinimumValidDateTo MinimumValidDateTo { get; private set; }
      public MinimumValidDateFrom MinimumValidDateFrom { get; private set; }
  
  
      public DiscountPercent DiscountPercent { get; private set; }
      public ValidDateTo ValidDateTo { get; private set; }
      public ValidDateFrom ValidDateFrom { get; private set; }
      public bool IsActive { get; private set; }
      public virtual DiscountType DiscountType { get; private set; }
      public virtual VitrinType VitrinType { get; private set; }
      public virtual ProductVariant ProductVariant { get; private set; }
  </code></pre>
  <p>I check the discount date when it is saved, which is not later than today's date:</p>
  <pre><code> public class ValidDateFrom : SharedKernel.Date
  {
      #region Static Member(s)
      public static FluentResults.Result&lt;ValidDateFrom&gt; Create(DateTime? value)
      {
          var result =
              new FluentResults.Result&lt;ValidDateFrom&gt;();
  
          if (value is null)
          {
              string errorMessage = string.Format
                  (Validations.Required, DataDictionary.ValidDateFrom);
  
              result.WithError(errorMessage: errorMessage);
  
              return result;
          }
  
          if (value.Value.Date &lt; SeedWork.Utility.Today)
          {
              string errorMessage = string.Format
                  (Validations.GreaterThanOrEqualTo_FieldValue,
                  DataDictionary.ValidDateFrom, DataDictionary.CurrentDate);
  
              result.WithError(errorMessage: errorMessage);
  
              return result;
          }
  
          var returnValue =
              new ValidDateFrom(value: value);
  
          result.WithValue(value: returnValue);
  
          return result;
      }
      #endregion /Static Member(s)
  
      /// &lt;summary&gt;
      /// For EF Core!
      /// &lt;/summary&gt;
      private ValidDateFrom() : base()
      {
      }
  
      private ValidDateFrom(DateTime? value) : base(value: value)
      {
      }
  }
  </code></pre>
  <p><strong>So far, so good.</strong></p>
  <p>The problem is when I want to see all the discounts in the site management section. When I want to read information from the database, it executes the above code again and because some discounts have expired, it does not allow me to get information from the database. I do not know what the problem is and why the Create method for ValidDateFrom should be run when fetching data.
  I get all the discounts with the help of the following code:</p>
  <pre><code>    public async Task&lt;List&lt;DiscountViewModel&gt;&gt; GetAllByDetailAsync()
      {
          try
          {
              var result = await DbSet
               .Include(x =&gt; x.ProductVariant)
               .ThenInclude(x =&gt; x.Product)
               .Select(x =&gt; new DiscountViewModel()
               {
                   Id = x.Id,
                   DiscountPercent = x.DiscountPercent.Value,
                   DiscountType = x.DiscountType.Value,
                   IsActive = x.IsActive,
                   MinimumPurchase = x.MinimumPurchase.Value,
                   ProductVariantId = x.ProductVariant.Id,
                   ValidDateFrom = x.ValidDateFrom.Value,
                   ValidDateTo = x.ValidDateTo.Value,
                   VitrinType = x.VitrinType.Value,
                   Inventory = x.ProductVariant.Inventory.Value,
                   ProductId = x.ProductVariant.Product.Id,
                   ProductName = x.ProductVariant.Product.ProductName.Value,
                   ProductVariantName = x.ProductVariant.Name.Value,
                   SellPrice = x.ProductVariant.SellPrice.Value - (x.ProductVariant.SellPrice.Value * x.DiscountPercent.Value.Value) / 100
               })
               .ToListAsync();
              return result;
  
          }
          catch (Exception me)
          {
  
              throw;
          }
  
      }
  </code></pre>