Base solution for your next web application
Starts in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

IEventHandler<EntityChangingEventData<PersonEmail>> Not Trigger If Value Object Updated #8801


User avatar
0
murat.yuceer created

Hello,

My entity have value object like below

public class PersonEmailConfiguration : IEntityTypeConfiguration<PersonEmail> { public void Configure(EntityTypeBuilder<PersonEmail> builder) { builder.ToTable("PersonEmails", SchemaNames.Person);

        builder.HasIndex(p => new { p.TenantId, p.PersonId });

        builder.Property(p => p.Id).ValueGeneratedNever();
        builder.Property(p => p.EmailTypeCode).IsRequired();
        builder.Property(p => p.IsPrimary).IsRequired();

        builder
            .HasOne(p => p.Person)
            .WithMany(p => p.Emails)
            .HasForeignKey(p => p.PersonId)
            .OnDelete(DeleteBehavior.NoAction);

        builder.HasOne(p => p.EmailType)
            .WithMany()
            .HasForeignKey(p => new { p.EmailTypeCode, p.TenantId })
            .HasPrincipalKey(p => new { p.Code, p.TenantId })
            .OnDelete(DeleteBehavior.NoAction);

        builder.OwnsOne(p => p.Email, p =>
        {
            p.Property(c => c.Address).IsRequired().HasColumnName("Email");
        });
    }
}

Its value object

   public class Email : ValueObject
{
    public string Address { get; }

    private Email() { }
    public Email(string address)
    {
        if (!address.Contains("@")) throw new Exception("Email is invalid");

        Address = address;
    }

    protected override IEnumerable&lt;object&gt; GetAtomicValues()
    {
        yield return Address;
    }
}

Problem is, if i update PersonEmail => Email => Address field predefined events not trigger auto I have to mark entity updated for fix problem with _personEmailRepository.UpdateAsync(email); Maybe this subject related about that => https://stackoverflow.com/questions/51416721/ef-core-owned-property-state-propagation-to-parent-entity


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

    Hi,

    Actually AspNet Boilerplate also uses a similar approach, see https://github.com/aspnetboilerplate/aspnetboilerplate/pull/4080/files.

    You can override ApplyAbpConcepts method of your DbContext (here) and see if entry.CheckOwnedEntityChange() returns true or false.

  • User Avatar
    0
    murat.yuceer created

    Thank you, will it fix next version of boilerplate?

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Did you find an error ? Did entry.CheckOwnedEntityChange() returned false ?

  • User Avatar
    0
    murat.yuceer created

    I didnt try yet, just i wonder why pull request waiting since 2018 :)

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    The PR I have shared is already merged.

  • User Avatar
    0
    murat.yuceer created

    Hi,

    I didnt check state but, im sure EntityChangingEventData not trigger if i update value object

  • User Avatar
    0
    maliming created
    Support Team

    hi @murat.yuceer

    Did you find an error ? Did entry.CheckOwnedEntityChange() returned false ?

    Can you check it out?

  • User Avatar
    0
    murat.yuceer created

    Hi, I checked, it was return false but i changed my code and its worked

    Before my code was like below

        public void SetPhone(Phone phone)
        {
            Check.NotNull(phone, nameof(phone));
    
            if (phone.ValueEquals(Phone))
                return;
    
            Phone = phone;
        }
       
    

    I changed like below and it worked

         public void SetPhone(Phone phone)
        {
            Check.NotNull(phone, nameof(phone));
    
            if (phone.ValueEquals(Phone))
                return;
    
            Phone.PhoneNumber = phone.PhoneNumber;
            Phone.CountryCode = phone.CountryCode;
            Phone.ExtensionNumber = phone.ExtensionNumber;
        }
    
  • User Avatar
    0
    maliming created
    Support Team

    Maybe ef is tracking the reference of the entity object.

    In short, your problem is solved. Right?

  • User Avatar
    0
    murat.yuceer created

    Yes, thanks for help