Adding [DataMember] [DataContract] attributes in Entity Framework POCO Template
Asked Answered
K

2

15

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.

Kristelkristen answered 9/9, 2010 at 15:39 Comment(0)
L
47

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
Leucopenia answered 9/9, 2010 at 15:53 Comment(7)
Thank you, exactly what i need, i think i might start understand T4 minute by minute, i just need some fixes quick right now. Please can you provide some help on where i add using namespaces on each entity?Kristelkristen
@gtas: updated my answer - you'll need to include the extra namespace System.Runtime.Serialization to handle the DataContract/DataMember attributesLeucopenia
Did you have any issues with serialization ? Especially regarding navigatonal properties, and issues with the dynamic proxy not being serialized.Advance
With EF 4.1, it's a little different, for the property fetch of the .tt, you have to add [DataMember] before the line who starts by : <#=accessibility#> <#=type#> <#=name#>Joust
I had to use [DataContract(IsReference = true)] because I was getting stuff like this in the logs: ...contains cycles and cannot be serialized if reference tracking is disabledJehol
is there anyway to edit the DbContext Code generated template, to include datacontract and datamember attribute so that I can use in data model in my WCF (SOAP) service?Middlebrooks
Helped me a lot!. However it was quite different for EF6, add [DataContract] above line# 27 and [DataMember] above line# 72. Also add using System.Runtime.Serialization; for using block.Breast
R
1

In EF6.1 you need to edit the model1.tt and add tag between UsingDirectives and EntityClassOpening:

<#=codeStringGenerator.UsingDirectives(inHeader: false)#>
//MY TAG:
[Serializable]
<#=codeStringGenerator.EntityClassOpening(entity)#>
Radices answered 5/6, 2014 at 19:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.