Page 1 of 1

Combine T4 Template

Posted: Mon 13 Feb 2017 19:17
by KW
I'm using your dbcontext T4 template to generate my entity classes.

I am trying to incorporate this framework with your dotconnect MySQL provider:

http://trackableentities.github.io/

Is it possible to combine their T4 template into your T4 template?

Here is the ENtityType.cs.t4 file that is used to generate the client side entities. Is there anyway to seamlessly merge this T4 template into your existing T4 template?

Code: Select all

<#@ template visibility="internal" linePragmas="false" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="Microsoft.Data.Entity.Design" #>
<#@ assembly name="EntityFramework" #>
<#@ import namespace="System.Data.Entity.Core.Metadata.Edm" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="Microsoft.Data.Entity.Design.CodeGeneration" #>
<#@ parameter type="System.Data.Entity.Core.Metadata.Edm.EntitySet" name="EntitySet" #>
<#@ parameter type="System.Data.Entity.Infrastructure.DbModel" name="Model" #>
<#@ parameter type="System.String" name="Namespace" #>
<#
    var code = new CSharpCodeHelper();
    var edm = new EdmHelper(code);

    if (EntitySet == null)
    {
        throw new ArgumentNullException("EntitySet");
    }

    if (Model == null)
    {
        throw new ArgumentNullException("Model");
    }

    var entityType = EntitySet.ElementType;
#>
namespace <#= Namespace #>
{
    using System;
    using System.Collections.Generic;
	using TrackableEntities.Client;

    public partial class <#= code.Type(entityType) #> : EntityBase
    {
<#
    var collectionProperties = from p in entityType.NavigationProperties
                               where p.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many
                               select p;

    if (collectionProperties.Any())
    {
#>
        public <#= code.Type(entityType) #>()
        {
<#
    foreach (var collectionProperty in collectionProperties)
    {
#>
            <#= code.Property(collectionProperty) #> = new ChangeTrackingCollection<<#= code.Type(collectionProperty.ToEndMember.GetEntityType()) #>>();
<#
    }
#>
        }

<#
    }

    var first = true;

    foreach (var property in entityType.Properties)
    {
        if (!first)
        {
            WriteLine(string.Empty);
        }
        else
        {
            first = false;
        }

#>
		public <#= code.Type(property) #> <#= code.Property(property) #>
		{ 
			get { return _<#= code.Property(property) #>; }
			set
			{
				if (Equals(value, _<#= code.Property(property) #>)) return;
				_<#= code.Property(property) #> = value;
				NotifyPropertyChanged(() => <#= code.Property(property) #>);
			}
		}
		private <#= code.Type(property) #> _<#= code.Property(property) #>;
<#
    }

    foreach (var navProperty in entityType.NavigationProperties)
    {
        if (!first)
        {
            WriteLine(string.Empty);
        }
        else
        {
            first = false;
        }

        if (navProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
        {
#>
		public ChangeTrackingCollection<<#= code.Type(navProperty.ToEndMember.GetEntityType()) #>> <#= code.Property(navProperty) #>
		{
			get { return _<#= code.Property(navProperty) #>; }
			set
			{
<#          if (navProperty.FromEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
            { #>
				if (value != null) value.Parent = this;
<#          } #>
				if (Equals(value, _<#= code.Property(navProperty) #>)) return;
				_<#= code.Property(navProperty) #> = value;
				NotifyPropertyChanged(() => <#= code.Property(navProperty) #>);
			}
		}
		private ChangeTrackingCollection<<#= code.Type(navProperty.ToEndMember.GetEntityType()) #>> _<#= code.Property(navProperty) #>;
<#
        }
		else
		{
#>

		public <#= code.Type(navProperty.ToEndMember.GetEntityType()) #> <#= code.Property(navProperty) #>
		{
			get { return _<#= code.Property(navProperty) #>; }
			set
			{
				if (Equals(value, _<#= code.Property(navProperty) #>)) return;
				_<#= code.Property(navProperty) #> = value;
				<#= code.Property(navProperty) #>ChangeTracker = _<#= code.Property(navProperty) #> == null ? null
					: new ChangeTrackingCollection<<#= code.Type(navProperty.ToEndMember.GetEntityType()) #>> { _<#= code.Property(navProperty) #> };
				NotifyPropertyChanged(() => <#= code.Property(navProperty) #>);
			}
		}
		private <#= code.Type(navProperty.ToEndMember.GetEntityType()) #> _<#= code.Property(navProperty) #>;
		private ChangeTrackingCollection<<#= code.Type(navProperty.ToEndMember.GetEntityType()) #>> <#= code.Property(navProperty) #>ChangeTracker { get; set; }
<#
        }
    }
#>
    }
}

Re: Combine T4 Template

Posted: Wed 15 Feb 2017 17:39
by Shalex
Entity Developer code generator system is based on its own object model. Its engine uses T4-like template language. So you cannot use standard T4 templates with Entity Developer. Refer to https://www.devart.com/entitydeveloper/ ... ation.html.

Please combine the results of both code generators (Visual Studio and Entity Developer) in your project.

Re: Combine T4 Template

Posted: Thu 16 Feb 2017 17:30
by KW
I was afraid you'd say that.