Many to many

97 views
Skip to first unread message

strangy

unread,
Feb 18, 2012, 9:00:27 PM2/18/12
to Simple.Data
Hello,

Is it possible with simple data to query a many to many relationship.
For example if I have User -> UserRoles -> Roles relationship is it
possible to populate a List<Roles> Roles property in the User class
with one query.

I have something like this

db.Users.Query().QueryByUsernameAndPassword(username, password)
.With(db.Users.UserRoles.Roles).FirstOrDefault<User>();

I get a User object with 5 (how? why?) empty Roles (I have 2 in the
database attached to the user I query).

If I use non generic FirstOrDefault i get an object that has a Roles
property but now only with the first role instead of both.

Am I asking to much from Simple.Data?

P.S.
Is it possoble for WithOne not to return a list but a single record ?

Regards, Ivica

Mark Rendle

unread,
Feb 20, 2012, 9:14:23 AM2/20/12
to simpl...@googlegroups.com
Hi,

Firstly, WithOne should return a single record and not a list, that is what it's for and if it's not doing that then I've made a boo-boo somewhere.

Secondly, yes, I think that a many-to-many is something that could be supported, and using the example you've provided, i.e. where you specify

.With(db.Table.JoinTable.OtherTable)

then Simple.Data can infer that you don't want the intermediate table in the result set.

I'll have a go at implementing this for beta2.

Cheers,
Mark

strangy

unread,
Feb 21, 2012, 2:23:06 PM2/21/12
to Simple.Data
Hello,

I have a one-to-one relation between Accomodations and
AccomodationDistances table.
When I use
db.Accomodations.All().WithOne(db.Accomodations.AccomodationDistances).ToList()
what i get is an object with an AccomodationDistances propertiy that
is a SimpleList not a SimpleRecord and if I
use .ToList<Accomodation>() I need to have a
List<AccomodationDistance> AccomodationDistances property which
correctly has only one item but the property should be
AccomodationDistance AccomodationDistances not a list.

You can find a test project for this at https://github.com/imunitic/SimpleDataWithOneReturnsList

Regards, Ivica

Mark Rendle

unread,
Feb 22, 2012, 5:52:53 AM2/22/12
to simpl...@googlegroups.com
Hi Ivica,

Again, thank you for the comprehensive repro code.

This behaviour was happening because the join was implicit from the referential integrity and the adapter ignored the WithType specified in the clause. Fix is done and will be in 1.0.0-beta2, which will be released shortly (as soon as I have finished some nifty optimisations around static type handling).

Cheers,
Mark

Oscar Alejandro Amado Sarmiento

unread,
May 15, 2013, 10:49:47 AM5/15/13
to simpl...@googlegroups.com
Hi, I try to do this to get data with a many to many relation

.With(db.Table.JoinTable.OtherTable)

but I dont have any result

Do you have any idea why?

Thanks

Mark Rendle

unread,
May 15, 2013, 10:51:56 AM5/15/13
to Simple.Data
Do you have primary and foreign keys on the tables?

Oscar Alejandro Amado Sarmiento

unread,
May 15, 2013, 10:55:44 AM5/15/13
to simpl...@googlegroups.com
Yes Mark,

I have primary key in Table and OtherTable and foreing keys in JoinTable

Mark Rendle

unread,
May 15, 2013, 11:42:40 AM5/15/13
to Simple.Data
Could you post a bit more detail, please? The full Simple.Data query and, if you can get it, the SQL output by the ADO adapter.

Mark Rendle
Founder & CEO
Oort Corporation
Makers of Zudio


--
You received this message because you are subscribed to the Google Groups "Simple.Data" group.
To unsubscribe from this group and stop receiving emails from it, send an email to simpledata+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Oscar Alejandro Amado Sarmiento

unread,
May 15, 2013, 7:05:58 PM5/15/13
to simpl...@googlegroups.com
Mark, 

I finally find the mistake. My c# class was:

public class MyClass
{
   public IEnumerable<MySecondClass> ClassInstance { get; set; }


I fix the problem using IList instead of IEnumerable

Mark Rendle

unread,
May 16, 2013, 4:41:30 AM5/16/13
to Simple. Data

Cool, glad you fixed it and thanks for letting us know.

Reply all
Reply to author
Forward
0 new messages