Page 1 of 1

cls.GetProperty doesn't work and don't find out why

Posted: Thu 28 Jun 2018 13:37
by Schulz_GdvKuhn
Hey Folks,
i want to create my Xml Mapping Files in different directories. So i added a Extended Property to Class:
Image

Then i add a folder name to this Property:
Image

Then i modified the NHibernate template:

Code: Select all

<#@ extended name="XmlMappingDir" owner="Class" type="System.String" description="Output directory for the mapping of this class." #>

Code: Select all

  if (XmlMappingAction != MetadataArtifactProcessing.DoNotGenerateMappingFiles) {
    output.Extension = ".hbm.xml";
    if (!XmlMappingFilePerClass) {
      if (XmlMappingAction == MetadataArtifactProcessing.CopyToOutputDirectory)
        output.PushOutputRedirection(XmlMappingOutput, model.FileName, BuildAction.None, CopyToOutputDirectory.CopyAlways);
      else
        output.PushOutputRedirection(XmlMappingOutput, model.FileName, BuildAction.EmbeddedResource, CopyToOutputDirectory.DoNotCopy);
#>
<#= ((EntityDeveloper.NHibernate.Serialization.HibernateModelSerializer)model.GetModelSerializer()).GetXmlMappingText(true) #><#
    output.PopOutputRedirection();
    }
    else {
      foreach(HibernateClass cls in model.Classes) {
	string xmlMappingDir = cls.GetProperty("XmlMappingDir") as string;
        if (string.IsNullOrEmpty(xmlMappingDir))
          xmlMappingDir = @"E:\GeneralMappings";

	XmlMappingOutput.DestinationFolder = xmlMappingDir;
        string rootFileName = ModelNameAsFilesPrefix ? baseFileName + "." + cls.Name : cls.Name;
        if (XmlMappingAction == MetadataArtifactProcessing.CopyToOutputDirectory)
          output.PushOutputRedirection(XmlMappingOutput, rootFileName, BuildAction.None, CopyToOutputDirectory.CopyAlways);
        else
          output.PushOutputRedirection(XmlMappingOutput, rootFileName, BuildAction.EmbeddedResource, CopyToOutputDirectory.DoNotCopy);
#>
<#= ((EntityDeveloper.NHibernate.Serialization.HibernateModelSerializer)model.GetModelSerializer()).GetClassXmlMappingText(cls) #><#
      output.PopOutputRedirection();
      }

      // save additional model mappings (filters and methods)
      if (model.Filters.Count > 0 || model.TypeDefs.Count > 0 || model.Methods.Count > 0) {
	  
	      if (XmlMappingAction == MetadataArtifactProcessing.CopyToOutputDirectory)
          output.PushOutputRedirection(XmlMappingOutput, model.FileName, BuildAction.None, CopyToOutputDirectory.CopyAlways);
        else
          output.PushOutputRedirection(XmlMappingOutput, model.FileName, BuildAction.EmbeddedResource, CopyToOutputDirectory.DoNotCopy);
#>
<#= ((EntityDeveloper.NHibernate.Serialization.HibernateModelSerializer)model.GetModelSerializer()).GetXmlMappingText(false) #><#
        output.PopOutputRedirection();

      }
    }
  }
After generation, all mapping files are in "E:\GeneralMappings" but "E:\SpecialMappings" is empty.
What did i overlook? Where is my mistake?

Greetings

Re: cls.GetProperty doesn't work and don't find out why

Posted: Mon 02 Jul 2018 10:14
by Shalex
1. Please use this code:

Code: Select all

    // Xml mapping generation
  if (XmlMappingAction != MetadataArtifactProcessing.DoNotGenerateMappingFiles) {
    output.Extension = ".hbm.xml";
    if (!XmlMappingFilePerClass) {
      if (XmlMappingAction == MetadataArtifactProcessing.CopyToOutputDirectory)
        output.PushOutputRedirection(XmlMappingOutput, model.FileName, BuildAction.None, CopyToOutputDirectory.CopyAlways);
      else
        output.PushOutputRedirection(XmlMappingOutput, model.FileName, BuildAction.EmbeddedResource, CopyToOutputDirectory.DoNotCopy);
#>
<#= ((EntityDeveloper.NHibernate.Serialization.HibernateModelSerializer)model.GetModelSerializer()).GetXmlMappingText(true) #><#
    output.PopOutputRedirection();
    }
    else {
      foreach(HibernateClass cls in model.Classes) {

	      string xmlMappingDir = cls.GetProperty("XmlMappingDir") as string;
        if (string.IsNullOrEmpty(xmlMappingDir))
          xmlMappingDir = @"E:\GeneralMappings";
  
          var outputInfo = new OutputInfo();
	        outputInfo.DestinationFolder = xmlMappingDir;

        string rootFileName = ModelNameAsFilesPrefix ? baseFileName + "." + cls.Name : cls.Name;
        if (XmlMappingAction == MetadataArtifactProcessing.CopyToOutputDirectory)
          output.PushOutputRedirection(outputInfo, rootFileName, BuildAction.None, CopyToOutputDirectory.CopyAlways);
        else
          output.PushOutputRedirection(outputInfo, rootFileName, BuildAction.EmbeddedResource, CopyToOutputDirectory.DoNotCopy);
#>
<#= ((EntityDeveloper.NHibernate.Serialization.HibernateModelSerializer)model.GetModelSerializer()).GetClassXmlMappingText(cls) #><#
      output.PopOutputRedirection();
      }

      // save additional model mappings (filters and methods)
      if (model.Filters.Count > 0 || model.TypeDefs.Count > 0 || model.Methods.Count > 0) {
	  
	      if (XmlMappingAction == MetadataArtifactProcessing.CopyToOutputDirectory)
          output.PushOutputRedirection(XmlMappingOutput, model.FileName, BuildAction.None, CopyToOutputDirectory.CopyAlways);
        else
          output.PushOutputRedirection(XmlMappingOutput, model.FileName, BuildAction.EmbeddedResource, CopyToOutputDirectory.DoNotCopy);
#>
<#= ((EntityDeveloper.NHibernate.Serialization.HibernateModelSerializer)model.GetModelSerializer()).GetXmlMappingText(false) #><#
        output.PopOutputRedirection();

      }
    }
  }
  // End of generation
2. Remove the XmlMappingDir property defined via Model Settings, because model's XmlMappingDir and template's XmlMappingDir do the same and may override each other. After XmlMappingDir is defined in template, save the model to make this property be available via Class Properties.