Many-to-many auditing in Entity Framework?

I read some articles about using RelationshipManager to access records that have related data. It is still unclear to me what is the best way to check when an object whose related data is added or updated.

Class examples:

public class Rfi
{
     public Guid Id {get;set;}
     public string Number {get;set;}
     public virtual ICollection<Attachment> Attachments {get;set;}
}

public Class Attachment
{
    public Guid Id {get;set;}
    public string Name {get;set;}
    public string Description {get;set;}
    public string FileName {get;set;}
    public string Path {get;set;}        
}

Matching Samples:

public class RfiMapping: EntityTypeConfiguration<Rfi>
{
     public Rfimapping()
     {
        HasMany(r => r.Attachments).WithMany().Map(m =>
                                                           {
                                                               m.MapLeftKey("RfiId");
                                                               m.MapRightKey("AttachmentId");
                                                               m.ToTable("Rfi_Attachments");
                                                           });
     }
}

I use repository templates and units of work. My UoW inherits from DbContext. A repository call might look like this:

public void AddAttachmentToRfi(Attachment attachment, Guid rfiId)
{
     var rfi = _rfiRepository.FindById(rfiId);
     rfi.Attachments.Add(attachment);
     _rfiRepository.UnitOfWork.Commit();
}

Is it possible, in the redefined SaveChanges method, to find out that the binding object is added to the Rfi object? When I cross, say, ChangeTracker.Entries, I don’t see its state being changed. This makes sense because I am only adding to relationships, not to the object directly.

, DbContext IObjectContextAdapter, , RelationshipManager, , - . , , , .

, Rfi (Rfi, , , ). , Rfi. , , . , , Rfi. , , .

+3
1

, .

EF Rfi_Attachments.

- , , , .

+1

All Articles