将[IgnoreDataMemeber]添加到生成的部分类中的特定属性 [英] Add [IgnoreDataMemeber] to specific properties in a generated partial class

查看:148
本文介绍了将[IgnoreDataMemeber]添加到生成的部分类中的特定属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由T4模板生成的实体框架POCO类。



我计划使用生成的类作为我的数据合同。但是,它有一些属性不需要在合同中。例如我有一个名为AddressId的属性。它是地址表的外键。由于实际地址附加到对象,我不希望AddressId对客户端可见。



我可以修改T4模板,但我宁愿不



我看到这个

  [MetadataType(typeof(Dinner_Validation))] 
public partial class Dinner {

public class Dinner_Validation
{
[必需]
public string标题{get;组; }
}

但是我尝试了我的类(使用[IgnoreDataMember])和它不能工作(AddressId仍然显示)。



如何隐藏这个属性,而不必使全新的类复制我的所有数据? p>

解决方案

MetadataType仅适用于数据注释。它不适用于序列化属性。序列化属性必须直接放在实体的属性中,所以如果要使用T4模板进行类生成,则必须添加直接创建模板的逻辑。



编辑:



如果要构建用于生成特殊属性的逻辑,您需要以某种方式告诉T4模板哪些属性应标记该属性。您可以将这些信息硬编码到T4模板中,也可以将该信息放入EDMX文件中。 EDMX文件支持其XML结构中的自定义数据元素。这些自定义XML元素可以在T4模板中用于一些额外的逻辑。唯一的问题是设计师没有支持 - 您必须将自定义元素直接添加到以XML格式打开的EDMX中。



这个整体称为结构注释。一些反向处理(修改数据库生成)的例子可以在我的其他答案(也检查MSDN主题链接在该问题)。还有整个扩展项目,这可能允许在设计器支持中添加新的扩展。我想您也可以在实体框架4中的操作书中阅读这些自定义。


I have an Entity Framework POCO class that is generated by a T4 Template.

I am planning to use the generated class as my data contract. However, it has a few properties that don't need to be in the contract. For example I have a property called AddressId. It is the foreign key to the address table. Since the actual address is attached to the object I don't want the AddressId to be visible to the client.

I could modify the T4 template, but I would rather not.

I saw this post that showed how to use 'MetadataType' to add attributes to existing properties in partial classes. This is the example that they gave:

[MetadataType(typeof(Dinner_Validation))] 
public partial class Dinner {} 

public class Dinner_Validation 
{ 
   [Required] 
   public string Title { get; set; } 
}

But I tried that for my class (using [IgnoreDataMember]) and it does not work (the AddressId is still shown).

How can I hide this one property without having to make a whole new class to copy all my data into?

解决方案

MetadataType works only with data annotations. It doesn't work with serialization attributes. Serialization attributes must be placed directly on properties in entity so if you want to use T4 template for class generation you must add logic for creating these attributes directly to template.

Edit:

If you want to build logic for generating special attributes you need somehow to tell T4 template which properties should be marked with the attribute. You can either hardcode such information into T4 template or you can put that information into EDMX file. EDMX file supports custom data elements in its XML structure. These custom XML elements can be latter used in T4 template for some additional logic. The only problem is that designer doesn't have support for that - you must add custom elements directly into EDMX opened as XML.

This whole is called Structural annotations. Some example with reverse processing (modifying database generation) can be found in my other answer (also check MSDN topic linked in the question). There is also whole extension project which perhaps allows adding new extensions together with designer support. I think you can also read about these customizations in Entity Framework 4 in Action book.

这篇关于将[IgnoreDataMemeber]添加到生成的部分类中的特定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆