LINQ-to-SQL and intersection entities

C

Craig Buchanan

I am trying to model a logical many-to-many relationship, defined as a two
'entity' tables and an intersection table, in LINQ-to-SQL. Thus far, this
has been represented as a User class, an Group class, and a UserGroup class
(the intersection). I would prefer to have the relationship modeled as:

User.Groups (a collection of Group instances) and Group.Users (a collection
of User instances)

How would I approach this? Suggestions are appreciated.

Thanks,

Craig Buchanan
 
M

Mr. Arnold

Craig Buchanan said:
I am trying to model a logical many-to-many relationship, defined as a two
'entity' tables and an intersection table, in LINQ-to-SQL. Thus far, this
has been represented as a User class, an Group class, and a UserGroup class
(the intersection). I would prefer to have the relationship modeled as:

User.Groups (a collection of Group instances) and Group.Users (a
collection of User instances)

How would I approach this? Suggestions are appreciated.

So why can't you just query the table with Linq with a result returned in a
Generic <List>? The Generic<List> would be a *collection* of
objects/entities.

Or why can't you use Stored Procedure that gives you the result set back
and create a Generic <List> from the result set, again it would be a
*collection* of objects/entities?
 
A

Anthony Jones

Craig Buchanan said:
I am trying to model a logical many-to-many relationship, defined as a two
'entity' tables and an intersection table, in LINQ-to-SQL. Thus far, this
has been represented as a User class, an Group class, and a UserGroup class
(the intersection). I would prefer to have the relationship modeled as:

User.Groups (a collection of Group instances) and Group.Users (a
collection of User instances)

How would I approach this? Suggestions are appreciated.

If you have the approriate foreign keys configured in the SQL schema then
when you build the Linq-To-SQL data context with the designer these
collection properties are included automatically.
 
C

Craig Buchanan

Anthony-

Thanks for the reply.

The User Entity does indeed have a UserGroups EntitySet. This implies that
I will need to create a new UserGroup (using the key values of the User and
the Group).

Instead, I was hoping to have a Groups EntitySet that would allow me to
assign a User to a Group by adding the Group to the User's Groups EntitySet.

Perhaps I can alter the User's partial class, expose a Groups property
(EntitySet Of Group), then somehow manage the add/removal using the
UserGroups EntitySet.

Thoughts?

Thanks,

Craig
 
C

Craig Buchanan

Thanks for the reply.

Currently, the User Entity exposes a UserGroups EntitySet. I know I can add
a User to the Group using this. I was hoping to somehow expose a Groups
EntitySet on the User Entity. I think a collection would make it more
challenging to persist changes to the collection.

Thoughts?

Thanks,

Craig
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top