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

Null reference exception when using FetchOneToMany #19

Closed
kevinsbennett opened this issue May 30, 2013 · 8 comments
Closed

Null reference exception when using FetchOneToMany #19

kevinsbennett opened this issue May 30, 2013 · 8 comments

Comments

@kevinsbennett
Copy link

I am getting an exception when running a OneToMany query where there are no 'many's'.

For example, I have the 2 following pocos:

[TableName("Products")]
[PrimaryKey("IDProduct")]
public partial class Product
{        
    public int IDProduct { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }

    [Ignore]
    public List<ProductCategory> ProductCategories { get; set; }
}

[TableName("ProductCategories")]
[PrimaryKey("IDProductCategory")]
public partial class ProductCategory
{        
    public int IDProductCategory { get; set; }
    public int IDProduct {get; set;}
}

and here is the query I am running:

var sql = @"select Products.*, ProductCategories.* 
              from Products left join ProductCategories on Products.IDProduct =     ProductCategories.IDProduct where Products.IDProduct=@0";

product = db.FetchOneToMany<Product, ProductCategory>(
     p => p.IDProduct, 
     pc => pc.IDProductCategory, 
     sql, id)
     .FirstOrDefault();

The manyKey lambda passed in is causing the problem... when there are no ProductCategories in the database, it gets a null reference exception because null is passed in for 'pc' into the lambda. When there are ProductCategories in the database, then the query works great.

Is this a bug or am I missing something?

@schotime
Copy link
Owner

Hey Keven,

This looks like a bug from first look. It is related to the change that was done so that if you are doing a many to one, and the one has all null values then the object will be set to null.

I will have a look at it today and hopefully have a solution for it.

Cheers,
Adam

@schotime
Copy link
Owner

Hi Keven,

Check out 2.1.25-beta on NuGet and let me know if it works.

Cheers,
Adam

@kevinsbennett
Copy link
Author

Adam,

that did the trick.

Thanks!
KB

@schotime
Copy link
Owner

schotime commented Jun 1, 2013

Excellent.
I think in theory now the second lambda is not needed.
This was to overcome the fact that an empty object would be mapped and not a null object returned.
Adam

@enriko-riba
Copy link

Having the same problem, any new release date?
Not that I couldn't use the beta but its cleaner to have only referenced release builds

@schotime
Copy link
Owner

Can create a release today probably.
Cheers,
Adam

@schotime
Copy link
Owner

This has been release in 2.1.27.
Thanks.

@enriko-riba
Copy link

Thats great thnx!

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

3 participants