以特定的属性添加

以特定的属性添加

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

问题描述

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

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

我计划使用生成的类作为我的数据合同。然而,它具有不需要在合同几个属性。比如我有一个名为AddressId属性。这是外键地址表中。由于实际地址连接到对象,我不希望AddressId是给客户端可见。

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.

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

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

我看到这个的的介绍了如何使用MetadataType将属性添加到部分类的现有属性。这是他们给的例子:

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; }
}



不过,我想,我的类(使用[IgnoreDataMember]),它(仍显示了AddressId)不起作用。

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仅适用于数据的注释。它不与序列的属性工作。 。序列化的属性必须直接放置在实体属性,所以如果你想使用类一代T4模板,您必须直接创建这些属性模板添加逻辑

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.

编辑:

如果你想建立逻辑产生你需要以某种方式告诉哪些属性应标有属性T4模板的特殊属性。你可以这样硬编码信息转化为T4模板或者你可以把这些信息输入EDMX文件。 EDMX文件支持在其XML结构的自定义数据元素。这些自定义XML元素可以是后者中的T4模板用于一些附加逻辑。唯一的问题是,设计人员不必为支持 - 你必须添加自定义元素直接进入EDMX开辟为XML

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.

这整个就是所谓的的。反向处理(修改数据库代)的一些例子可以在的(也检查问题挂钩MSDN主题)。还有这或许允许与设计师的支持添加新的扩展。我想你也可以阅读有关在行动书实体框架4这些定制。

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]在生成的局部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!