C#/ NHibernate:关联引用未映射的类:MvcApplication7.Models.Comments

时间:2014-09-15 15:27:48

标签: c# asp.net-mvc nhibernate

我无法弄清楚为什么我会收到此错误: 关联引用未映射的类:MvcApplication7.Models.Comments。 请注意:一个帖子可以有很多评论(一对多关系)

我对所有这些技术都很陌生。你能告诉我我做的是对的吗?


        namespace MvcApplication7.Models
        {
            public class Posts
            {
                public virtual int Id { get; set; }
                public virtual int PostId { get; set; }
                public virtual string Post { get; set; }

            }

        }

    ********************************************************************

        namespace MvcApplication7.Models
        {
            public class Comments
            {
                 public virtual int CommentId { get; set; }
                 public virtual int PostId { get; set; }
                 public virtual string Comment { get; set; }
            }
        }

    ********************************************************************

        <?xml version="1.0" encoding="utf-8" ?>
        <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="MvcApplication7" namespace="MvcApplication7.Models">

          <class name="Posts" table="Posts" dynamic-update="true" >
            <cache usage="read-write"/>
            <id name="Id" column="Id" type="int">
              <generator class="sequence">
                <param name="sequence">posts_id_seq</param>
              </generator>
            </id>

            <property name="Post" column="Post"/>

            <set name="PostId" inverse="true" lazy="true" >
              <key column="PostId"/>
              <one-to-many class="Comments"/>
            </set>

          </class>
        </hibernate-mapping>

    ********************************************************************

        <?xml version="1.0" encoding="utf-8" ?>
        <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" assembly="MvcApplication7" namespace="MvcApplication7.Models">

          <class name="Comments" table="Comments" dynamic-update="true" >
            <cache usage="read-write"/>

            <id name="CommentId" column="CommentId" type="int">
              <generator class="sequence">
                <param name="sequence">comments_commentid_seq</param>
              </generator>
            </id>

            <property name="Comment" column="Comment"/>

            <many-to-one name="Post" class="MvcApplication7.Models.Posts" column="Id"></many-to-one>

          </class>
        </hibernate-mapping>
*****************************************************************************

以下是我在PostgraSQL中的表格:

CREATE TABLE comments
(
  commentid serial NOT NULL,
  postid integer,
  comment text,
  CONSTRAINT commentid PRIMARY KEY (commentid),
  CONSTRAINT postid FOREIGN KEY (postid)
      REFERENCES posts (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITH (
  OIDS=FALSE
);
ALTER TABLE comments
  OWNER TO postgres;
*********************************************************
CREATE TABLE posts
(
  id serial NOT NULL,
  post text,
  CONSTRAINT id PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);
ALTER TABLE posts
  OWNER TO postgres;

1 个答案:

答案 0 :(得分:1)

要遵循数据库设计,这些应该是实体:

public class Post
{
    public virtual int Id { get; set; }
    public virtual string PostText { get; set; }
    public virtual IList<Comment> Comments { get; set; }
}

public class Comment
{
     public virtual int Id { get; set; }
     public virtual Post Post { get; set; }
     public virtual string CommentText { get; set; }
}

映射

  <class name="Post" table="Posts" dynamic-update="true" >
    <cache usage="read-write"/>
    <id name="Id" column="Id" type="int">
      <generator class="sequence">
        <param name="sequence">posts_id_seq</param>
      </generator>
    </id>

    <property name="PostText" column="Post"/>

    <bag name="Comments" inverse="true" lazy="true" >
      <key column="PostId"/>
      <one-to-many class="Comment"/>
    </bag>

  </class>



  <class name="Comment" table="Comments" dynamic-update="true" >
    <cache usage="read-write"/>

    <id name="Id" column="CommentId" type="int">
      <generator class="sequence">
        <param name="sequence">comments_commentid_seq</param>
      </generator>
    </id>

    <property name="CommentText" column="Comment"/>

    <many-to-one name="Post" class="Post" column="PostId" />

  </class>

因此,我们使用单个表单名称评论为每个评论和帖子为每个帖子

此外,Post还收集了这些评论。它们通过Comments表中的PostId列映射为一对多。相同的列用于Comment上Post属性(父)的多对一映射。如果您创建了注释

,请不要忘记设置这两种关系
Post post = ...
Comment comment = new Comment() ...
post.Comments.Add(comment);
comment.Post = post;

这是反映射的必要条件

一定要仔细检查:

Chapter 21. Example: Parent/Child