0

I am struggling to find a simple answer to this question, hopefully someone out there can help?

I have a system using MVC3 code first and EF4.1.

I have a number of models and I am trying to override DbContext.SaveChanges to provide an audit facility.

There are certain high volume columns that should be excluded from the Audit.

I had hoped that I could use the AdditionalMetadata tag like so...

public class User : IAuditable
{
    [Key]
    public int UserID { get; set; }
    public string Name { get; set; }
    public string DisplayName { get; set; }
    [AdditionalMetadata("IgnoreAudit", true)]
    public DateTime? LastActive { get; set; }
}

and then in my audit code use something like...

bool AuditThis = ModelMetadata
   .FromLambdaExpression(dbEntry.Property(propertyName), null)
   .AdditionalValues("IgnoreAudit");

to determine whether to log the change or not.

Obviously this code fails as it was taken (and changed!) from a view.

My question is. Can the ModelMetaData be read outside of a ViewContext or am I barking up the wrong tree?

Thanks for taking the time to read.

1 Answer 1

0

I found the pointer on stackoverflow here but I needed the slightly different

var metaData = ModelMetadataProviders
    .Current.GetMetadataForProperty(null, objType, propertyName);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.