映射多对多关系的问题(通过代码映射)

发布于 2024-11-19 12:11:46 字数 2208 浏览 1 评论 0原文

我正在通过代码使用新的 NHibernate 3.2 映射(不流畅的 nhibernate),并且在映射多对多关系时遇到一些问题。

我想映射这些实体

 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; }
    }

我的映射看起来

  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>
        {
            public 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);
                }));
            }
        }

唯一的问题是,当我使用此模式在数据库中生成表时,我有两个附加表。我必须更改什么才能禁用生成这两个表(文章和标签)? 在此处输入图像描述

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>
        {
            public 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)?
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰火雁神 2024-11-26 12:11:46

你真的需要这个吗?:

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

Do you really need this?:

 public class ArticleTagMapping : ClassMapping<ArticleTag>
        {
            public ArticleTagMapping()
            {
                ManyToOne<Article>(x => x.Article, x => { });
                ManyToOne<Tag>(x => x.Tag, x => { });
            }
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文