7.1.19

changer le modèle .tt pour l'entity framework

le modèle complet peut être télécharger ici :


pour supprimer la création du constructeur sans paramètre, éliminer les lignes :

public <#=code.Escape(entity)#>()...

pour insérer le FirePropertyChanged, rechercher la ligne :

public string Property(EdmProperty edmProperty)

et remplacer toute la méthode par :

public string Property(EdmProperty edmProperty)
    {
    var propertyName = _code.Escape(edmProperty);
    var finalValue = "value"; // not modified by default

return string.Format(
CultureInfo.InvariantCulture,
"private {1} {6};" + 
Environment.NewLine + "\t{0} {1} {2}" + 
Environment.NewLine + "\t{{" + 
Environment.NewLine + "\t\t{3}get" +
Environment.NewLine + "\t\t{{" +
Environment.NewLine + "\t\t\treturn {6};" +
Environment.NewLine + "\t\t}}" +
Environment.NewLine + "\t\t{4}set" +
Environment.NewLine + "\t\t{{" +
Environment.NewLine + "\t\t\t{6} = {7};" +
Environment.NewLine + "\t\t\tFirePropertyChanged();" +
Environment.NewLine + "\t\t}}" +
Environment.NewLine + "\t}}" + Environment.NewLine,
Accessibility.ForProperty(edmProperty),
_typeMapper.GetTypeName(edmProperty.TypeUsage),
propertyName,
_code.SpaceAfter(Accessibility.ForGetter(edmProperty)),
_code.SpaceAfter(Accessibility.ForSetter(edmProperty)),
"\"" + _code.Escape(edmProperty) + "\"",
"_" + FirstLetterToLower(_code.Escape(edmProperty)),
finalValue);
    }

puis :

public string NavigationProperty(NavigationProperty navProp)

et remplacer toute la méthode par :

public string NavigationProperty(NavigationProperty navigationProperty)
    {
        var endType = _typeMapper.GetTypeName(navigationProperty.ToEndMember.GetEntityType());
var one = "";
var six = "";
if (navigationProperty.ToEndMember.RelationshipMultiplicity == RelationshipMultiplicity.Many)
{
one = "ObservableCollection<" + endType + ">";
six = "";
else
{
one = endType;
if (one == _code.Escape(navigationProperty))
{
six = Environment.NewLine + "\t\t\t" + _code.Escape(navigationProperty) + "Id = ((" + _code.Escape(navigationProperty) + ")value).Id;";
}
else
{
six = "";
}
}

        return string.Format(
            CultureInfo.InvariantCulture,
            "{0} {1} {5};" + 
Environment.NewLine + "\tpublic virtual {1} {2} {{{3} " + 
Environment.NewLine + "\t\tget" + 
Environment.NewLine + "\t\t{{" + 

Environment.NewLine + "\t\t\treturn {5};" + 
Environment.NewLine + "\t\t}} {4}" + 
Environment.NewLine + "\t\tset" + 
Environment.NewLine + "\t\t{{" + 
Environment.NewLine + "\t\t\t{5} = value;" +
"{6}" +
Environment.NewLine + "\t\t\tFirePropertyChanged();" +
Environment.NewLine + "\t\t}}" + 
Environment.NewLine + "\t}}",
            "private",
            one,
            _code.Escape(navigationProperty),
            _code.SpaceAfter(Accessibility.ForGetter(navigationProperty)),
            _code.SpaceAfter(Accessibility.ForSetter(navigationProperty)),
"_" + FirstLetterToLower(_code.Escape(navigationProperty)),
""
);
    }

rechercher la ligne :

<#=codeStringGenerator.EntityClassOpening(entity)#>

puis la modifier en :

<#=codeStringGenerator.EntityClassOpening(entity)#> : INotifyPropertyChanged

rechercher la ligne :

this.<#=code.Escape(navigationProperty)#> = new HashSet

puis changer HashSet en ObservableCollection

rechercher la ligne :

<#=codeStringGenerator.EntityClassOpening(entity)#> : INotifyPropertyChanged
{

et dans l'implémentation de la méthode (au début) ajouter :

    public event PropertyChangedEventHandler PropertyChanged;

    private void FirePropertyChanged([CallerMemberName] string propertyName = null)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

rechercher la ligne :

public string UsingDirectives(bool inHeader, bool includeCollections = true)

et à la ligne "{2}", changer en :

"{2}" +
"using System.Collections.ObjectModel;" + Environment.NewLine +
"using System.ComponentModel;" + Environment.NewLine +
 "using System.Runtime.CompilerServices;",

puis ajouter la méthode suivante :

public static string FirstLetterToLower(string text)
{
return Char.ToLowerInvariant(text[0]) + text.Substring(1);
}

Aucun commentaire: