Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Self referencing table (ID, ParentID) #248

Open
onyx84 opened this issue Dec 27, 2010 · 0 comments
Open

Self referencing table (ID, ParentID) #248

onyx84 opened this issue Dec 27, 2010 · 0 comments

Comments

@onyx84
Copy link

onyx84 commented Dec 27, 2010

I am using ActiveRecord

I have a company table with fields:

Id | ParentId | CompanyName | Description
ParentId referencing to Id, which creates a parent child relationship.

the generated Code is incorrect?

from T4 template ActiveRecord.tt

    public IQueryable<<#=fk.OtherClass #>> <#=propName #>
    {
        get
        {

              var repo=<#=Namespace #>.<#=fk.OtherClass#>.GetRepo();
              return from items in repo.GetAll()
                   where items.<#=CleanUp(fk.OtherColumn)#> == _<#=CleanUp(fk.ThisColumn)#>
                   select items;
        }
    }

will generate :

    public IQueryable<Company> Companies
    {
        get
        {

              var repo=EMS.Data.Company.GetRepo();
              return from items in repo.GetAll()
                   where items.Id == _ParentId
                   select items;
        }
    }

I added Checking if the table is referencing itself:

    public IQueryable<<#=fk.OtherClass #>> <#=propName #>
    {
        get
        {

              var repo=<#=Namespace #>.<#=fk.OtherClass#>.GetRepo();
              return from items in repo.GetAll()
                   <# if(fk.OtherClass != tbl.ClassName) { #>
                   where items.<#=CleanUp(fk.OtherColumn)#> == _<#=CleanUp(fk.ThisColumn)#>
                   <#}else {#>
                   where items.<#=CleanUp(fk.ThisColumn)#> == _<#=CleanUp(fk.OtherColumn)#>
                   <#}#>
                   select items;
        }
    }

which generates this code:

    public IQueryable<Company> Companies
    {
        get
        {

              var repo=EMS.Data.Company.GetRepo();
              return from items in repo.GetAll()
                                          where items.ParentId == _Id
                                          select items;
        }
    }

which i think is correct since Companies represent the Child company of my entity.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant