I have writen a LocationInterceptionAspect using PostSharp. It should run in a MVC5 applicacyion with EF6, when the setter or getter of a class of the EF model is called. The aspect is called correctly when I add the annotation to the auto-generated model class. As this is not a valid option I tried to add the annotation to the respective metadatatype class.
This works:
public partial class Company
{
[Encrypt]
public string name{ get; set; }
}
This does not work:
[MetadataType(typeof(CompanyMetadata))]
public partial class Company
{
}
internal sealed class CompanyMetadata
{
[Encrypt] // does not work
[Requiered] // works
public string name{ get; set; }
}
In case I add other annotations as the [Requiered] annotation the functionallity of this annotation will be executed. I have tried to place the [Encrypt] annotation in other places - it always works. Only in the metadatatype class I have the problem that the aspect is not called.
Someone has an idea? Thanks for help!
Entity Framework uses the type specified in
[MetadataType]as a source for metadata. Logic of properties within the Metadata type does not matter.Since PostSharp changes the implementation of those properties you will not see any difference in behavior.
What you need to do is to apply the aspect directly on the property of generated class
Company. Since you do not have access to the source code (it's generated), you need to "multicast" the aspect on all properties you need.The most easiest way is to do following:
This tells PostSharp to apply the location-level aspect to properties named "name" on the target class. Since the class is present in the assembly, it will be changed correctly.