In your user mapping:

<set name="Roles" table="UserRoles">
    <key column="UserId"/>
    <element column="Role"
type="NHibernate.Type.EnumStringType`1[[YourNamespace.RoleEnum,
YourAssembly]]"/>
</set>

your user class:

public class User
{
    private ICollection<Role> roles = new HashSet<Role>();
...
    public ICollection<Role> Roles { get { return roles; } }
}


You don't need to map roles.

2011/3/9 David <[email protected]>

> Thanks, I've had a think about this now and drawn out a few potential
> diagrams.  Imagining my previous scenario of Categories, Books and
> Publishers and the desire to add Notes.
>
> Database wise, there are three ways I see of accomplishing this.
> Option 1 is a notes table for each item, i.e. BookNotes, CategoryNotes
> and the associated C# classes for this.  No difficulty with code, but
> ugly and inextensible.
>
> Option 2 is one notes table and a mapping table for each, e.g.
>
> public class Book
> {
>  public virtual string Title { get; set; }
>  public virtual List<Note> Notes { get; set; }
> }
>
> public class Note
> {
>  public virtual string Text { get; set; }
>  public virtual List<Book> Books { get; set; }
>  public virtual List<Author> Authors { get; set; }
>  // .. etc
> }
>
> I assume from your post that this is what you meant by the HasMany,
> and I assume that this is how you would implement the Note class (with
> multiple lists).  Can you confirm that the example implementation
> above is correct?
>
> Option 3 would be some form of class that any item which supports
> "notes" can derive from, with a mapping table i.e.:
>
> ItemFk | ItemType | NoteFk
> 1 | Book | 2
> 4 | Author | 2
> ...
>
> I suspect this is a many-to-any, but have no idea how to implement
> this in C# code or whether it is supported like this by nhibernate.
> This seems to be the most flexible way of implementing notes/tags/
> categories that I can think of, but obviously requires a bit more
> code.  If this isn't impossible or crazy, can anybody suggest how many-
> to-any would look in a few C# classes/interfaces?
>
> Thanks for your help.
>
> David
>
>
> On Mar 6, 8:14 pm, "Drew Jones" <[email protected]> wrote:
> > The HasMany convention should help you set this up. Author, Book, and
> > Publisher. If you plan on attaching the SAME note to several of these,
> use
> > HasManyToMany.
> >
> >
> > -----Original Message-----
> > From: David
> > Sent: Sunday, March 06, 2011 12:53 PM
> > To: nhusers
> > Subject: [nhusers] Many-to-many different classes
> >
> > I am using nhibernate but I seem to be making my life harder and
> > harder as I plan my design.
> >
> > I have a bunch of domain objects which are linked together in an
> > entirely normal way.  For the purposes of example, these could be
> > Authors, Books and Publishers.
> >
> > However I would like to extend this so that these items can have other
> > attributes linked to them, e.g. Notes.  An individual Book could have
> > multiple notes, as could an Author or Publisher.  The same sort of
> > system could work for categories, tags[1] or a bunch of other
> > attributes.
> >
> > Is there a sensible way of designing a domain model (and database
> > structure) around this without having a mapping table for each?  I
> > have not seen any examples where one domain model (e.g. Notes) has
> > multiple lists of other objects that could relate to it.
> >
> > I have only seen the following, which could get complicated pretty
> > quickly and introduces a whole unnecessary layer of complexity:
> >
> > Author <-> AuthorNotes <-> Notes
> > Book <-> BookNotes <-> Notes
> > Publisher <-> PublisherNotes <-> Notes
> >
> > It would be great to hear about alternative ways of defining these
> > sorts of relationships.
> >
> > 1 - I know there are alternative methods of implementing this and will
> > be considering them.
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nhusers" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group athttp://
> groups.google.com/group/nhusers?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "nhusers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/nhusers?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"nhusers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/nhusers?hl=en.

Reply via email to