简体   繁体   中英

Unusual? HasManyToMany NHibernate Mapping

Database Strucutre:

Shows
ID
Name

Genres
ID
Name

ShowsGenres
ShowsID
GenresID

Above is my Database I am trying to figure out how to map this properly. My Show object is like this:

public class Show
{
    public virtual int ID { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<Genre> Genres { get; set; }
}

My Genre Object is:

public class Genre
{
    public virtual int ID { get; set; }
    public virtual string Name { get; set; }
    public virtual IList<Show> Shows { get; set; }
}

I have tried several different varations of HasManyToMany, but none work the way I want them to.

I need to be able to delete a show and the relationship with the genre, or many genres, but not delete the genre(s).

I need to be able to delete a genre and its relationship with a show, or many shows, but not delete the show(s).

How can I map this or do I need to try something differently?

Update: Also thinking about it more I would also need to be able to remove the relationship between a show and a genre without removing the show or the genre.

Here are my mappings I have, but not exactly sure they are correct.

        HasManyToMany<Genre>(x => x.Genres)
            .Table("ShowGenres")
            .ParentKeyColumn("ShowID")
            .ChildKeyColumn("GenreID");

        HasManyToMany<Show>(x => x.Shows)
            .Table("ShowGenres")
            .ParentKeyColumn("GenreID")
            .ChildKeyColumn("ShowID");

This is an old post but I basically had the same question. Answered here:

HasManyToMany Fluent NHibernate Mapping Delete Error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM