1

I am trying to use T4 to generate code for the following:

public virtual IList<Frequency> GetAll()
    {
        using (var wc = new WCDataClassesDataContext())
        {
            return wc.Frequencies.ToList();
        }
    }

    public virtual IList<Frequency> GetAll(Expression<Func<Frequency, bool>> whereClause)
    {
        using (var wc = new WCDataClassesDataContext())
        {
            return wc.Frequencies.Where(whereClause).ToList();
        }
    }

In T4 i am using:

foreach (var entity in typeMapper.GetItemsToGenerate<EntityType>(itemCollection))
{
    fileManager.StartNewFile(entity.Name + "BaseFinder.cs");
    BeginNamespace(code);
 #>

<#=codeStringGenerator.UsingDirectives(inHeader: false)#>
using System.Linq;
using System.Linq.Expressions;
<#=codeStringGenerator.EntityClassOpening(entity)#>
{

    public virtual IList<<#=entity.Name #>> GetAll()
    {
        using (var wc = new WCDataClassesDataContext())
        {
            return wc.[[Frequencies]].ToList();
        }
    }

    public virtual IList<<#=entity.Name #>> GetAll(Expression<Func<<#=entity.Name #>, bool>> whereClause)
    {
        using (var wc = new WCDataClassesDataContext())
        {
            return wc.[[Frequencies]].Where(whereClause).ToList();
        }
    }

......
}

Instead of [[Frequencies]] i want the main table name that the entity is mapped to. I am trying to setup various getters which can be used easily in the classes.

Could you tell me what is the solution to do this, or there could be some other way to do this?

Hope you got my point.

Thanks.

Umair
  • 4,864
  • 3
  • 28
  • 47

1 Answers1

0

It looks like you have the entity type already, so I think you're close - all you need to do is get the navigation properties that have a relationship multiplicity of many, and you should be set.

Something like:

EntityType et = entity.GetType();
foreach(var navProp in et.NavigationProperties.Where(np=>np.DeclaringType == et 
        && np.RelationshipMultiplicity == RelationshipMultiplicity.Many))
{
    string s = string.Format("public virtual IList<{0}> GetAll() ...", 
                           navProp.ToEndMember.GetEntityType().Name);

}

The Entity Framework DB-first generator already does a flavor of this; if you go digging around under the EDMX, you should see a *.Context.tt or something similar. In there, do a search for NavigationProperty, there is a code string helper method to generate something similar.

Ross
  • 2,448
  • 1
  • 21
  • 24
  • Yup this can help i guess, will have a look into this. Is there any resource where I can find all the helper methods or class descriptions / documentation of classes used in T4? – Umair Apr 04 '13 at 10:51
  • I haven't been able to find one, but that doesn't mean one doesn't exist. I've had the best luck digging through the `*.Context.tt` files that VS adds after you add an EDMX and the EF Utility `ttinclude`, usually located in `C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes`. – Ross Apr 04 '13 at 15:14
  • Also, for what it's worth, you can do this in a non-T4 context using `MetadataWorkspace`. [This](http://stackoverflow.com/questions/10817289/entity-framework-finding-foreign-keys) question might be of help. – Ross Apr 04 '13 at 15:22