简体   繁体   English

使用带有后处理属性拦截的EF元数据类型模型类不起作用

[英]using EF metadatatype model class with postsharp property interception not working

I have writen a LocationInterceptionAspect using PostSharp. 我已经使用PostSharp编写了LocationInterceptionAspect。 It should run in a MVC5 applicacyion with EF6, when the setter or getter of a class of the EF model is called. 当调用EF模型的类的setter或getter时,它应在带有EF6的MVC5应用程序中运行。 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. 如果我添加其他注释作为[Requiered]注释,则将执行此注释的功能。 I have tried to place the [Encrypt] annotation in other places - it always works. 我尝试将[Encrypt]批注放置在其他位置-它始终有效。 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. 实体框架使用[MetadataType]指定的类型作为[MetadataType]的来源。 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. 由于PostSharp更改了这些属性的实现,因此您在行为上不会看到任何差异。

What you need to do is to apply the aspect directly on the property of generated class Company . 您需要做的是将方面直接应用于生成的类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: 最简单的方法是执行以下操作:

[Encrypt(AttributeTargetMembers = "name", 
         AttributeTargetElements = MulticastTargets.Property)]
public partial class Table
{
}

This tells PostSharp to apply the location-level aspect to properties named "name" on the target class. 这告诉PostSharp将位置级别方面应用于目标类上名为“ name”的属性。 Since the class is present in the assembly, it will be changed correctly. 由于类存在于装配中,因此将正确更改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM