I'm working on a .Net core project targeted .Net 5. I have a method that will receive a parameter his type is Expression<Func<T , object[]>>, inside the method I will loop on all returned properties from the expression.
What I trie :
public virtual void UpdateExcept( TEntity record, params Expression<Func<TEntity , object[]>>[] propertiesToBeExcluded )
{
   //Some logic here
    foreach ( var property in  propertiesToBeExcluded )
    {
        foreach ( var prop in property.GetMemberAccessList() )
        {
          //Here I got the property name (I think)
           var x = prop.Name;
        }
    }
}
More explain: In reality I created this method In a repository based on Entity framework this method should update an TEntity record and ignore (do not update) some sent properties in the propertiesToBeExcluded sometimes I will update a record and ignore one property and in another times I will update one record and ignore many properties.
Originale method logic I tried:
public virtual void UpdateExcept( TEntity record, params Expression<Func<TEntity , object[]>>[] propertiesToBeExcluded )
{
    var entity = Context.Set<TEntity>();
    entity.Attach( record );
    Context.Entry( record ).State = EntityState.Modified;
    foreach ( var property in  propertiesToBeExcluded )
    {
        foreach ( var prop in property.GetMemberAccessList() )
        {
            Context.Entry( record ).Property( prop.Name ).IsModified = false;
        }
    }
}
Example of using for this method:
_studentRepository.UpdateExcept( record : student , propertiesToBeExcluded : x => new object[] {x.Picture} );
Another example:
_studentRepository.UpdateExcept( record : student , propertiesToBeExcluded : x => new object[] {x.CreatedOn, x.CreatedBy} );
This method was in this structure:
public virtual void UpdateExcept( TEntity record, params Expression<Func<TEntity , object>>[] propertiesToBeExcluded )
{
    var entity = Context.Set<TEntity>();
    entity.Attach( record );
    Context.Entry( record ).State = EntityState.Modified;
    foreach ( var property in  propertiesToBeExcluded )
    {
        Context.Entry( record ).Property( property ).IsModified = false;
    }
}
Example of use for the old structure:
_studentRepository.UpdateExcept( record : student , propertiesToBeExcluded : x => x.CreatedOn, x => x.CreatedBy );
Why I changed from the old structure to the new: Cause if you notice in the old structure I have to write many times the x's parameter for the func and I was no idea how to use it one time and return multiple properties.
I wish all this helped you understand the issue, so please any help about this issue ?
 
Aucun commentaire:
Enregistrer un commentaire