开发者

problem with mapping a many-to-many relation (mapping by code)

开发者 https://www.devze.com 2023-03-19 10:46 出处:网络
I\'m using a new to NHibernate 3.2 mapping by code (not fluent nhibernate) and I have a little problem with mapping a many-to-many relation.

I'm using a new to NHibernate 3.2 mapping by code (not fluent nhibernate) and I have a little problem with mapping a many-to-many relation.

I want to map these entities

 public class Article
    {
        public virtual Guid Id { set; get; }
        public virtual string Content { set; get; }
        public virtual string Title { set; get; }
        public virtual IList<Tag> Tags { set; get; }
    }
 public class Tag
    {
        public virtual Guid Id { set; get; }
        public virtual string Name { set; get; }
        public virtual IList<Article> Articles { set; get; }
    }

    public class ArticleTag
    {
        public virtual Article Article { set; get; }
        public virtual Tag Tag { set; get; }
    }

My mapping looks that

  public class TagMapping : ClassMapping<Tag>
        {
            public TagMapping()
            {
                Id<Guid>(x => x.Id);
                Property<string>(x => x.Name);
                Bag<Article>(x => x.Articles, x => x.Inverse(true), x => x.ManyToMany(z =>
                {
                    z.Column("Article");
                    z.Lazy(LazyRelation.Proxy);
                }));
            }
        }
        public class ArticleTagMapping : ClassMapping<ArticleTag>
        {
            p开发者_Python百科ublic ArticleTagMapping()
            {
                ManyToOne<Article>(x => x.Article, x => { });
                ManyToOne<Tag>(x => x.Tag, x => { });
            }
        }
       public class ArticleMapping : ClassMapping<Article>
        {
            public ArticleMapping()
            {
                Id<Guid>(x => x.Id, x => x.Generator(Generators.Guid));
                Property<string>(x => x.Content, x => x.Length(4002));
                Property<string>(x => x.Title);
                Bag<Tag>(x => x.Tags, x =>{ }, x => x.ManyToMany(z =>
                {
                    z.Column("Tag");
                    z.Lazy(LazyRelation.Proxy);
                }));
            }
        }

The only problem is that, when I generate tables in database using this schema, I have two additional tables. What I must change to disable generating these two tables (Articles and Tags)?

problem with mapping a many-to-many relation (mapping by code)


Do you really need this?:

 public class ArticleTagMapping : ClassMapping<ArticleTag>
        {
            public ArticleTagMapping()
            {
                ManyToOne<Article>(x => x.Article, x => { });
                ManyToOne<Tag>(x => x.Tag, x => { });
            }
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号