Base solution for your next web application
Open Closed

Error on update #2763


User avatar
0
anelengqandu created

Hi Guys

I keep on getting this error every time I do an update. "System.InvalidOperationException: Attaching an entity of type 'Synapsis.SmartRentals.RentalContracts.RentalContract' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate." Below is my update code

var property = Mapper.Map<Property>(input);
  await _propertyRepository.UpdateAsync(property);

3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you share your input and entity classes ?

    Thanks.

  • User Avatar
    0
    anelengqandu created

    My entity

    [Table("SrProperties")]
        public class Property : FullAuditedEntity, IPassivable
        {
            public const int MaxDisplayAsLength = 128;
            public const int MaxDescriptionLength = 2048;
    
            [Required]
            [StringLength(MaxDisplayAsLength)]
            public virtual string DisplayAs { get; set; }
            /// <summary>
            /// Describes the property
            /// </summary>
            [StringLength(MaxDescriptionLength)]
            public virtual string Description { get; set; }
            public virtual string Notes { get; set; }
            public string StreetNumber { get; set; }
            public virtual string Address { get; set; }
            public virtual string Suburb { get; set; }
            public virtual string City { get; set; }
            public virtual string Province { get; set; }
            public virtual string Country { get; set; }
            public virtual string Code { get; set; }
            public virtual int Furnished { get; set; }
            public virtual string FullAddress { get; set; }
    
    
            public virtual DbGeography Location { get; set; }
            public virtual double Latitude { get; set; }
            public virtual double Longitude { get; set; }
    
          
            public virtual string ErfNumber { get; set; }
    
            public virtual DateTime DateListed { get; set; }
    
            public virtual int ErfSize { get; set; }
    
            public virtual int FloorSize { get; set; }
    
            public virtual double MinimumRent { get; set; }
    
            /// <summary>
            /// Rooms
            /// </summary>
            public virtual int BedRoom { get; set; }
            public virtual int LivingRoom { get; set; }
            public virtual int Lounge { get; set; }
            public virtual int DiningRoom { get; set; }
            public virtual int ReceptionRoom { get; set; }
            public virtual int BathRoom { get; set; }
            public virtual int Kitchen { get; set; }
            public virtual int OfficeStudy { get; set; }
            public virtual int EntranceHall { get; set; }
    
            ///<summary>
            ///External Features
            ///</summary>       
            public virtual int Garages { get; set; } //start from 0
            public virtual bool UndercoverParking { get; set; }
            public virtual bool Pool { get; set; }
            public virtual bool Garden { get; set; }
            public virtual bool Outbuilding { get; set; }
            [ForeignKey("RoofTypeId")]
            public RoofType RoofType { get; set; }
            public virtual int RoofTypeId { get; set; }
    
            [ForeignKey("WindowTypeId")]
            public WindowType WindowType { get; set; }
            public virtual int WindowTypeId { get; set; }
            public virtual int SmokingAllowed { get; set; }
            public virtual int PetsAllowed { get; set; }
            public virtual bool SecurityAvailable { get; set; }
            public virtual string Other { get; set; }
            public virtual int LeaseTerm { get; set; }
    
    
            /// <summary>
            /// Lease period in months
            /// </summary>
            public virtual LeasePeriods LeasePeriod { get; set; }
            public virtual LeaseFrequency LeaseFrequency { get; set; }
            public virtual SmokingRules SmokingRules { get; set; }
    
            public virtual double DepositAmountRequired { get; set; }
    
            public virtual string MunicipalAccountNumber { get; set; }
    
            public virtual double RatesAndTaxesAmount { get; set; }
    
            public int? ParentId { get; set; }
            public virtual Property Parent { get; set; }
            public virtual ICollection<Property> Children { get; set; }
            public virtual ICollection<PropertyUnit> PropertyUnits { get; set; }
            public virtual ICollection<RentalContract> PropertyContract { get; set; }
    
            [ForeignKey("PropertyTypeId")]
            public virtual PropertyType PropertyType { get; set; }
            public virtual int PropertyTypeId { get; set; }
    
            /// <summary>
            /// Land Lord
            /// </summary>
            [ForeignKey("LandlordId")]
            public virtual Peoples.People Landlord { get; set; }
            public virtual int? LandlordId { get; set; }
    
            public virtual bool IsActive { get; set; }
    
            public Property()
            {
                DateListed = DateTime.Today;
                IsActive = true;
            }
        }
    

    My Input

    [AutoMap(typeof(Property))]
        public class PropertyDto : EntityDto
        {
            public string DisplayAs { get; set; }
            public virtual int CreatorUserId { get; set; }
            /// <summary>
            /// Describes the property
            /// </summary>
            public string Description { get; set; }
            public string Notes { get; set; }
            public string StreetNumber { get; set; }
            public virtual string Address { get; set; }
            public string Suburb { get; set; }
            public virtual string City { get; set; }
            public virtual string Province { get; set; }
            public virtual string Country { get; set; }
            public virtual string FullAddress { get; set; }
            public virtual string Code { get; set; }
            public virtual int PropertyTypeId { get; set; }
            public virtual string PropertyTypeName { get; set; }
            public virtual DbGeography Location { get; set; }
            public virtual double Latitude { get; set; }
            public virtual double Longitude { get; set; }
            public virtual string ErfNumber { get; set; }
            public DateTime DateListed { get; set; }
            public int ErfSize { get; set; }
            public int FloorSize { get; set; }
            public double MinimumRent { get; set; }
            public virtual int LandLordId { get; set; }
            public virtual string LandlordFirstName { get; set; }
            public virtual string LandlordLastName { get; set; }
    
    
    
            public virtual string LandlordDisplayName => LandlordLastName + " " + LandlordFirstName;
    
            public virtual string LandlordEmailAddress { get; set; }
            public virtual string LandlordCity { get; set; }
            public virtual string LandlordCellNumber { get; set; }
            public virtual string LandlordFilename { get; set; }
    
    
            public string LandlordFacebook { get; set; }
            public string LandlordTwitter { get; set; }
            public string LandlordLinkedin { get; set; }
    
            public string TenantFacebook { get; set; }
            public string TenantTwitter { get; set; }
            public string TenantLinkedin { get; set; }
    
            public virtual int LeaseTerm { get; set; }
            public virtual int LeasePeriod { get; set; }
            public double DepositAmountRequired { get; set; }
            public string MunicipalAccountNumber { get; set; }
            public double RatesAndTaxesAmount { get; set; }
    
            /// <summary>
            /// SocialNets
            /// </summary>
            public string Facebook { get; set; }
            public string Twitter { get; set; }
            public string Linkedin { get; set; }
    
    
            /// <summary>
            /// Feartures \ Rooms
            /// </summary>  
            public virtual int BedRoom { get; set; }
            public virtual int LivingRoom { get; set; }
            public virtual int Lounge { get; set; }
            public virtual int DiningRoom { get; set; }
            public virtual int ReceptionRoom { get; set; }
            public virtual int BathRoom { get; set; }
            public virtual int Kitchen { get; set; }
            public virtual int OfficeStudy { get; set; }
            public virtual int EntranceHall { get; set; }
            public virtual int Furnished { get; set; }
            public virtual int LeaseFrequency { get; set; }
    
            ///<summary>
            ///External Features
            ///</summary>
            public virtual int Garages { get; set; } //start from 0
            public virtual bool UndercoverParking { get; set; }
            public virtual bool Pool { get; set; }
            public virtual bool Garden { get; set; }
            public virtual bool Outbuilding { get; set; }
            public virtual int RoofTypeId { get; set; }
            public virtual int WindowTypeId { get; set; }
            public virtual int SmokingAllowed { get; set; }
            public virtual int PetsAllowed { get; set; }
            public virtual bool SecurityAvailable { get; set; }
            public virtual string Other { get; set; }
            public List<PropertyUnitDto> PropertyUnits { get; set; }
            public List<ContractDto> PropertyContract { get; set; }
            public virtual int CountUnits => PropertyUnits?.Count ?? 0;
            public virtual int CountPropertyContracts => PropertyContract?.Count ?? 0;
            public bool IsActive { get; set; }
    
            public PropertyDto()
            {
                Address = "";
                Suburb = "";
                City = "";
                Code = "";
                Province = "";
                Country = "";
                IsActive = true;
                DateListed = DateTime.Now;
                RoofTypeId = 1;
                WindowTypeId = 1;
            }
    
        }
    
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Lastly, can you share the data you send from client to server for this field ?

    public List<ContractDto> PropertyContract { get; set; }