.net – One to one relationships with Subsonic

netsubsonic

How can you create a one to one relationship in Subsonic? For example, I have my table called Readings and each Reading has only one book, however Subsonic returns an IQueryable of books. I want it to return only one book. Thanks.

Best Answer

I'm assuming you're using the ActiveRecord template to generate your code.

Inside ActiveRecord.tt, you'll see a section that looks like this:

 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;
            }
        }

This is the template for the code that is generated for every foreign key on your table. You will need to modify this section and perhaps put some logic around this code and generate something different for your one-to-one keys.

Hope that points you in the right direction.