0

我有一个包含关键字的表。我有几个其他表可以有多个关键字(从关键字表中提取)。这是我所拥有的,它有效,但感觉好像有更好的方法来映射它。我只是不确定它会是什么(如果有的话)

public class Keyword
{
    public virtual int Id {get; protected set;}
    public virtual string Name {get; set;}
}

public class KeywordMap : ClassMap<Keyword>
{
    public KeywordMap()
    {
        Id(x => x.Id).Not.Nullable().GeneratedBy.Identity();
        Map(x => x.Name).Not.Nullable.Length(100);
    }
}

public class Article
{
    public virtual int Id {get; protected set;}
    //... other properties omitted
    public virtual IList<Keyword> Keywords {get; set;}
}

public class ArticleMap : ClassMap<Article>
{
    public ArticleMap()
    {
        Id(x => x.Id).Not.Nullable().GeneratedBy.Identity();
        HasMany(x => x.Keywords).Inverse().Cascade.Delete();
    }
}

public class Picture
{
    public virtual int Id {get; protected set;}
    //... other properties omitted
    public virtual IList<Keyword> Keywords {get; set;}
}

public class PictureMap : ClassMap<Picture>
{
    public PictureMap()
    {
        Id(x => x.Id).Not.Nullable().GeneratedBy.Identity();
        HasMany(x => x.Keywords).Inverse().Cascade.Delete();
    }
}
4

1 回答 1

0

从片段中我不确定你想要实现什么......似乎有一个表'[Keywords]',它有两列'Picture_Id'和'Article_Id'。

所以它要么用于 aPicture要么用于Article(其他引用为空)。这也可能意味着“名称”列中有多个相同的值......如果我没看错的话

我想many-to-many在这种情况下更合适的是关系。单套独一无二Keyword.Name。然后每个关系都会有配对表[ArticleKeyword] [PictureKeyword]

如果是这种情况,我们可以像这样映射它(参见Fluent NHibernate HasManyToMany() MappingFluent NHibernate - HasManyToMany...

public ArticleMap()
{
    Id(x => x.Id).Not.Nullable().GeneratedBy.Identity();
    HasManyToMany<Keyword>(x => x.Keyword)
      .Table("ArticleKeyword")
      .ParentKeyColumn("Article_Id") // lot of mapping could be omited 
      .ChildKeyColumn("Keyword_Id")  // thanks to conventions 
      ;

更多关于多对多的阅读

于 2013-06-11T12:15:45.057 回答