在Entity Framework POCO模板中添加[DataMember] [DataContract]属性 [英] Adding [DataMember] [DataContract] attributes in Entity Framework POCO Template

查看:210
本文介绍了在Entity Framework POCO模板中添加[DataMember] [DataContract]属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一些帮助添加在POCO .tt实体框架模板属性支持WCF序列化,如果它可能如何添加命名空间使用每个实体。



谢谢。

解决方案

您需要打开 model1.tt 文件



找到这些代码行(第34行,在我的例子中):

  //发出实体类型
foreach(ItemCollection.GetItems中的EntityType实体< EntityType>()。OrderBy(e = Name))
{
fileManager.StartNewFile(entity.Name +.cs);

在此行之前:

 <#= Accessibility.ForType(entity)#> <#= code.SpaceAfter(code.AbstractOption(entity))#>部分类<#= code.Escape(entity)#><#= code.StringBefore(:,code.Escape .BaseType))#> 

您需要添加 [DataContract] 属性:

  [DataContract] 
<#= Accessibility.ForType(entity)#> <#= code.SpaceAfter(code.AbstractOption(entity))#> partial class<#= code.Escape(entity)#><#= code.StringBefore(:,code.Escape .BaseType))#>


找到此行代码:

 <#= PropertyVirtualModifier(Accessibility.ForProperty(edmProperty))#> <#= code.Escape(edmProperty.TypeUsage)#> <#= code.Escape(edmProperty)#> 

在此行之前,添加 [DataMember] 属性到您的模板:

  [DataMember] 
<#= PropertyVirtualModifier(Accessibility.ForProperty(edmProperty)) #> <#= code.Escape(edmProperty.TypeUsage)#> <#= code.Escape(edmProperty)#>

这将为所有实体和所有属性添加这些属性 - 这可能不是你真正想要的,但它是一个开始。



另外:要完成,你可能想看看 WriteHeader 函数在您的T4模板中,并添加使用System.Runtime.Serialization; 语句到您的使用列表:

  using System.Collections.ObjectModel; 
using System.Collections.Specialized;
using System.Runtime.Serialization; // add this to handle DataContract / DataMember


I would like some help adding in a POCO .tt Entity Framework template Attributes to support WCF serialization and if its possible how to add namespace usings to each entity.

Thank you.

解决方案

You need to open your model1.tt file (or whatever it's called in your project), and add a few things.

Locate these lines of code (line 34, in my case):

// Emit Entity Types
foreach (EntityType entity in ItemCollection.GetItems<EntityType>().OrderBy(e => e.Name))
{
    fileManager.StartNewFile(entity.Name + ".cs");

Just before this line here:

<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#><#=code.StringBefore(" : ", code.Escape(entity.BaseType))#>

you need to add the [DataContract] attribute:

[DataContract]
<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#><#=code.StringBefore(" : ", code.Escape(entity.BaseType))#>

to your template.

Next, locate this line of code:

<#=PropertyVirtualModifier(Accessibility.ForProperty(edmProperty))#> <#=code.Escape(edmProperty.TypeUsage)#> <#=code.Escape(edmProperty)#>

Before that line, add the [DataMember] attribute to your template:

[DataMember]
<#=PropertyVirtualModifier(Accessibility.ForProperty(edmProperty))#> <#=code.Escape(edmProperty.TypeUsage)#> <#=code.Escape(edmProperty)#>

This will add those attributes for all entities and all properties - that might not be what you really want, but it's a start.

Also: to finish off, you probably want to have a look at the WriteHeader function in your T4 template, and add the using System.Runtime.Serialization; statement to your list of usings:

using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Runtime.Serialization;  // add this to handle DataContract/DataMember

这篇关于在Entity Framework POCO模板中添加[DataMember] [DataContract]属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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