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.
views:
216answers:
2I'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.
What I did was create a partial class like so:
public partial class Reading
{
private Book _book;
public Book Book
{
get
{
if (_book == null)
_book = this.Books.SingleOrDefault();
{
return _book;
}
}
set
{
_book = value;
}
}
}
Then you can access Reading's child object Book directly, instead of through the Iqueryable object.
If anyone knows of a better way to do this, please let me know as this is all I could come up with after a lot of struggling.
Edit: Forgot to mention that this partial class must be in the same namespace as the generated Subsonic models