I have a Data Model generated by Entity Framework using DataBase First approach. This dataModel is located in a WCF Service and is returned by multiple methods to the client. In the client application, the type CreationType
is declared in the service reference ( by updating the service reference ).
// This code was generated from a template.
[DataContract]
public partial class CreationType
{
public CreationType()
{
this.Quotes = new HashSet<Quote>();
}
[DataMember]
public int CreationTypeId { get; set; }
[DataMember]
public string Code { get; set; }
[DataMember]
public string NameFr { get; set; }
[DataMember]
public string NameEn { get; set; }
}
The question is: Is there a way to regenerate the model without loosing WCF attributes? I mean, can they be declared in a non-generated file and be apply to the generated like in a partial class or something?
I don't necessary need all properties being DataMember and I don't want every EntityModel being DataContract.
To achieve this you can edit template from which EF classes are generated. By editing T4 template you can control behavior of EF class generator.
You are looking for file *.tt
or to be more specific: YourModelName.tt
Inside this file you can find a rules along with EF classes are generated.
Here are lines that should be changed:
[DataContract]
<#=Accessibility.ForType(entity)#> <#=code.SpaceAfter(code.AbstractOption(entity))#>partial class <#=code.Escape(entity)#><#=code.StringBefore(" : ", code.Escape(entity.BaseType))#>
Next line is:
[DataMember]
<#=PropertyVirtualModifier(Accessibility.ForProperty(edmProperty))#> <#=code.Escape(edmProperty.TypeUsage)#> <#=code.Escape(edmProperty)#>
You probably gonna need to add also this line:
<#@ import namespace="System.Runtime.Serialization" #>
in this section:
<#@ import namespace="System.Text.RegularExpressions" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="System.Diagnostics" #>