Open Closed

While trying to delete a record from context getting System.InvalidOperationException : The property 'Cusotmer.ExtraProperties' could not be found. Ensure that the property exists and has been included in the model. #8327


User avatar
0
Karunakar created
  • ABP Framework version: latest
  • UI Type: Angular
  • Database System: EF Core (MySQL.)
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes/no
  • Exception message and full stack trace: While trying to delete a record from context getting System.InvalidOperationException : The property 'Cusotmer.ExtraProperties' could not be found. Ensure that the property exists and has been included in the model.
  • Steps to reproduce the issue:
    1. Create and Entity by using AggregateRoot<Customer, Guid> and also have ISoftDelete interface.
    1. While model creation we igonred ExtraProperties and dont want this property in our table.
    1. try to delete(set isDelete=true) a record based on ISoftDelete by DeleteAsync method from repository using IRepository<> interface.
    1. Will get Exception System.InvalidOperationException : The property 'Cusotmer.ExtraProperties' could not be found. Ensure that the property exists and has been included in the model.
    1. We need the IsoftDelete needs to update the record filed IsDelete=0 without expecting the property ExtraProperties.

Note : While deleting we are using IHasDelettionTime interface for entity so that it will updating the DeletionTime filed but we need DeletorId like CreatorId while deleting the record to identity who deleted the record. *


9 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    could you share your entity config in the dbcontext?

  • User Avatar
    0
    Karunakar created
    private static void ConfigureVendor(ModelBuilder builder)
     {
         builder.Entity<Vendor>(b =>
         {
             // Get the entity type
             var entityType = typeof(Vendor);
             // Retrieve the Table attribute from the entity type
             var tableAttribute = entityType.GetCustomAttributes(typeof(TableAttribute), false)
                                            .Cast<TableAttribute>()
                                            .FirstOrDefault();
    
             // Extract the table name
             var tableName = tableAttribute?.Name ?? entityType.Name;
    
             //Configure table & schema name
             b.ToTable(tableName, VendorManagementDbProperties.DbSchema);
    
             b.ConfigureByConvention();
             b.Ignore(q => q.ExtraProperties);
             b.Ignore(q => q.Id);
    
             // Primary Key Configuration
             b.HasKey(v => v.VendorId);
    
             // Properties Configuration
             b.Property(v => v.FirstName)
                 .HasMaxLength(VendorConsts.FirstNameLength)
                 .IsRequired();
    
             b.Property(v => v.LastName)
                 .HasMaxLength(VendorConsts.LastNameLength)
                 .IsRequired();   
              
             b.Property(v => v.BillingAccountNumber)
                 .HasMaxLength(VendorConsts.BillingAccountNumberLength);
    
             b.Property(v => v.Is1099)
                 .IsRequired().HasDefaultValue(false);
    
             b.Property(v => v.IsW9OnFile)
                 .IsRequired().HasDefaultValue(false);
    
             b.Property(v => v.Active)
                 .IsRequired().HasDefaultValue(true);
    
             b.Property(q => q.CreatorId)
                 .IsRequired();
    
             b.Property(q => q.CreationTime).HasDefaultValueSql("CURRENT_TIMESTAMP")
                 .IsRequired();
    
             b.Property(q => q.IsDeleted)
                 .HasDefaultValue(false);
    
             b.HasOne(v => v.AchInformation)
                 .WithMany()  // Assuming a one-to-many relationship with AchInformation
                 .HasForeignKey(v => v.ACHInfoId)
                 .OnDelete(DeleteBehavior.SetNull);
    
             // Store the enum value as an integer in the database
             b.Property(v => v.TypeOfVendorId)
                 .HasConversion<int>(); // This ensures EF Core treats the enum as an integer value in the database
    
         });
     }
    
  • User Avatar
    0
    Karunakar created

    this is my Entity

    [Table("Vendor")]
    public class Vendor : AggregateRoot<Guid>, IHasCreationTime, IMustHaveCreator, IMultiTenant, ISoftDelete, IHasDeletionTime
    {
        public Vendor()
        {
            Active = true;
            Is1099 = false;
            IsW9OnFile = false;
            IsDeleted = false;
        }
      
        [Key]
        public Guid VendorId { get; set; }
        [Required]
        [StringLength(VendorConsts.FirstNameLength, MinimumLength = VendorConsts.MinLength)]
        public string FirstName { get; set; }
    
        [Required]
        [StringLength(VendorConsts.LastNameLength, MinimumLength = VendorConsts.MinLength)]
        public string LastName { get; set; }
    
        [StringLength(VendorConsts.VendorNameLength, MinimumLength = VendorConsts.MinLength)]
        public virtual string? VendorName { get; set; }
    
       // public virtual int? TypeOfVendorId { get; set; }
    
        //Navigation Property to the VendorId entity
        [ForeignKey("TypeOfVendorId")]
        public virtual TypeOfVendors TypeOfVendorId { get; set; }  // Navigation property
    
        [StringLength(VendorConsts.DbaFsoNameLength, MinimumLength = VendorConsts.MinLength)]
        public virtual string? DbaFsoName { get; set; }
    
        [StringLength(VendorConsts.PayToNameLength, MinimumLength = VendorConsts.MinLength)]
        public virtual string? PayToName { get; set; }
    
        [StringLength(VendorConsts.VendorNumberLength, MinimumLength = VendorConsts.MinLength)]
        public virtual string? VendorNumber { get; set; }
    
        [StringLength(VendorConsts.SSN_FederalTaxIdLength, MinimumLength = VendorConsts.MinLength)]
        public virtual string? SSN_FederalTaxId { get; set; }
    
        [Required]
        public int TaxType { get; set; }
    
        [StringLength(VendorConsts.BillingAccountNumberLength, MinimumLength = VendorConsts.MinLength)]
        public virtual string? BillingAccountNumber { get; set; }
    
        public virtual Guid? PaymentTerm { get; set; }
    
        [Required]
        public bool Is1099 { get; set; }
    
        [Required]
        public bool IsW9OnFile { get; set; }
    
        public virtual int? TypeOf1099 { get; set; }
    
        public virtual Guid? ACHInfoId { get; set; }
    
        // Navigation Property to the VendorId entity
        [ForeignKey("ACHInfoId")]
        public virtual ACHInfo AchInformation { get; set; }  // Navigation property
    
        public bool Active { get; set; }
    
        public virtual Guid? DefaultJobIdProjectDivision { get; set; }
    
        public virtual Guid? DefaultGlAccount { get; set; }
    
        public virtual Guid? DefaultLineNumber { get; set; }
    
        public virtual Guid? DefaultSeries { get; set; }
    
        public virtual Guid? DefaultLocation { get; set; }
    
        public virtual Guid? DefaultSet { get; set; }
    
        public virtual Guid? DefaultInsRef { get; set; }
    
        public virtual Guid? DefaultFF1 { get; set; }
    
        public virtual Guid? DefaultFF2 { get; set; }
    
        public virtual Guid? DefaultFF3 { get; set; }
    
        public virtual Guid? DefaultFF4 { get; set; }
    
        public virtual Guid? Notes { get; set; }
    
        public virtual int? Currency { get; set; }
    
        public virtual Guid? AdvanceAccount { get; set; }
    
        public virtual Guid? PcardAdvanceAccount { get; set; }
    
        [StringLength(VendorConsts.StudioVendorNumberLength, MinimumLength = VendorConsts.MinLength)]
        public virtual string? StudioVendorNumber { get; set; }
    
        [StringLength(VendorConsts.CustomerNumberLength, MinimumLength = VendorConsts.MinLength)]
        public virtual string? CustomerNumber { get; set; }
    
        public virtual int? BusinessCategory { get; set; }
    
        [Required]
        public int Ethnicity { get; set; }
      
        [Required]
        public virtual bool IsThirdPartyVendor { get; set; }
    
        public virtual Guid? TenantId { get; set; }
    
        public virtual Guid CreatorId { get; set; }
    
        [Column(TypeName = "datetime")]
        public virtual DateTime CreationTime { get; set; }
    
        public bool IsDeleted { get; set; }
    
        public virtual Guid? DeletedByUser { get; set; }
    
        [Column(TypeName = "datetime")]
        public virtual DateTime? DeletionTime { get; set; }
    
        public Vendor(
            Guid vendorId, string firstName, string lastName, string? vendorName, TypeOfVendors typeOfVendorId,
            string? dbaFsoName, string? payToName, string? vendorNumber, string? sSN_FederalTaxId,
            int taxType, string? billingAccountNumber, Guid? paymentTerm,
            bool is1099, bool isW9OnFile, int? typeOf1099, Guid? aCHInfoId,
            bool active, Guid? defaultJobIdProjectDivision, Guid? defaultGlAccount,
            Guid? defaultLineNumber, Guid? defaultSeries, Guid? defaultLocation,
            Guid? defaultSet, Guid? defaultInsRef, Guid? defaultFF1, Guid? defaultFF2,
            Guid? defaultFF3, Guid? defaultFF4, Guid? notes, int? currency,
            Guid? advanceAccount, Guid? pcardAdvanceAccount, string? studioVendorNumber,
            string? customerNumber, int? businessCategory, int ethnicity,
            bool isThirdPartyVendor, Guid? tenantId, Guid creatorId,
            DateTime createDateTime, bool isDeleted, Guid? deletedByUser, DateTime? deletionTime)
        {
            VendorId = vendorId;
            FirstName = firstName;
            LastName = lastName;
            VendorName = vendorName;
            TypeOfVendorId = typeOfVendorId;
            DbaFsoName = dbaFsoName;
            PayToName = payToName;
            VendorNumber = vendorNumber;
            SSN_FederalTaxId = sSN_FederalTaxId;
            TaxType = taxType;
            BillingAccountNumber = billingAccountNumber;
            PaymentTerm = paymentTerm;
            Is1099 = is1099;
            IsW9OnFile = isW9OnFile;
            TypeOf1099 = typeOf1099;
            ACHInfoId = aCHInfoId;
            Active = active;
            DefaultJobIdProjectDivision = defaultJobIdProjectDivision;
            DefaultGlAccount = defaultGlAccount;
            DefaultLineNumber = defaultLineNumber;
            DefaultSeries = defaultSeries;
            DefaultLocation = defaultLocation;
            DefaultSet = defaultSet;
            DefaultInsRef = defaultInsRef;
            DefaultFF1 = defaultFF1;
            DefaultFF2 = defaultFF2;
            DefaultFF3 = defaultFF3;
            DefaultFF4 = defaultFF4;
            Notes = notes;
            Currency = currency;
            AdvanceAccount = advanceAccount;
            PcardAdvanceAccount = pcardAdvanceAccount;
            StudioVendorNumber = studioVendorNumber;
            CustomerNumber = customerNumber;
            BusinessCategory= businessCategory;
            Ethnicity = ethnicity;
            IsThirdPartyVendor = isThirdPartyVendor;
            TenantId = tenantId;
            CreatorId = creatorId;
            CreationTime = createDateTime;
            IsDeleted = isDeleted;
            DeletedByUser = deletedByUser;
            DeletionTime = deletionTime;
        }
    }
    
  • User Avatar
    0
    Karunakar created
    public enum TypeOfVendors
    {
        Standard,
        CreditCard,
        Pcard,
        Pc,
        TaxAgency,
        PayrollCompany,
        ShippingPartner,
        Pdecm,
        Director,
        SalesRep,
        Messenger,
        CarService,
        Phones,
        Bidder,
        ExecutiveProducer,
        DirectorOfPhotography,
        Producer
    
    }
    
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    You can try override the ApplyAbpConceptsForDeletedEntity method

    YourDbContext

    protected override void ApplyAbpConceptsForDeletedEntity(EntityEntry entry)
    {
        if (!(entry.Entity is ISoftDelete))
        {
            return;
        }
    
        if (IsHardDeleted(entry))
        {
            return;
        }
    
        if (entry.CurrentValues.Properties.Any(x => x.Name == nameof(IHasExtraProperties.ExtraProperties)))
        {
            ExtraPropertyDictionary? originalExtraProperties = null;
            if (entry.Entity is IHasExtraProperties)
            {
                originalExtraProperties = entry.OriginalValues.GetValue<ExtraPropertyDictionary>(nameof(IHasExtraProperties.ExtraProperties));
            }
    
            entry.Reload();
    
            if (entry.Entity is IHasExtraProperties)
            {
                ObjectHelper.TrySetProperty(entry.Entity.As<IHasExtraProperties>(), x => x.ExtraProperties, () => originalExtraProperties);
            }
        }
        
    
        ObjectHelper.TrySetProperty(entry.Entity.As<ISoftDelete>(), x => x.IsDeleted, () => true);
        SetDeletionAuditProperties(entry);
    }
    
  • User Avatar
    0
    Karunakar created

    I will try by applying the above changes but what about DeletorId to update in DB for delete.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    You can implement IDeletionAuditedObject interface

  • User Avatar
    0
    Karunakar created

    Hi Liangshiwei, I have tried with above code but the entire record is deleted instead softdelete(IsDelete=0).

     private readonly IVendorRepository _VendorRepository;
     public async Task DeleteAsync(Guid id)
     {
         await _VendorRepository.DeleteAsync(x=>x.VendorId == id);
     }
    
     public interface IVendorRepository : IRepository<Vendor, Guid>, ITransientDependency
     {
     }
    
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Try this

    protected override void ApplyAbpConceptsForDeletedEntity(EntityEntry entry)
    {
        if (!(entry.Entity is ISoftDelete))
        {
            return;
        }
    
        if (IsHardDeleted(entry))
        {
            return;
        }
    
        if (entry.CurrentValues.Properties.Any(x => x.Name == nameof(IHasExtraProperties.ExtraProperties)))
        {
            ExtraPropertyDictionary? originalExtraProperties = null;
            if (entry.Entity is IHasExtraProperties)
            {
                originalExtraProperties = entry.OriginalValues.GetValue<ExtraPropertyDictionary>(nameof(IHasExtraProperties.ExtraProperties));
            }
    
            ;
    
            if (entry.Entity is IHasExtraProperties)
            {
                ObjectHelper.TrySetProperty(entry.Entity.As<IHasExtraProperties>(), x => x.ExtraProperties, () => originalExtraProperties);
            }
        }
        
        entry.Reload()
        ObjectHelper.TrySetProperty(entry.Entity.As<ISoftDelete>(), x => x.IsDeleted, () => true);
        SetDeletionAuditProperties(entry);
    }
    
Made with ❤️ on ABP v9.1.0-preview. Updated on December 13, 2024, 06:09