使用ASP.NET MVC和NHibernate更新外键

本文关键字:更新 NHibernate ASP NET MVC 使用 | 更新日期: 2023-09-27 18:21:59

我的应用程序中有一个父子关系,例如一篇帖子可以有多条评论(一对多关系)。我已经完成了映射的代码,我需要写一个代码,这样当我把一个孩子插入数据库时,它也会插入父ID。目前,我的代码不插入父id,只插入子id的其他详细信息。有人能帮我解开我的代码吗?

型号:

namespace MvcApplication7.Models
{
    public class Post
    {
        public virtual int Id { get; set; }
        [Display(Name = "Post")] 
        public virtual string PostText { get; set; }
        public virtual IList<Comment> Comments { get; set; }
    }
}
namespace MvcApplication7.Models
{
    public class Comment
    {
        public virtual int Id { get; set; }
        public virtual Post Post { get; set; }
        [Display(Name = "Comments")] 
        public virtual string CommentText { get; set; }
    }
}

NHibenate映射:

    <?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="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>
    </hibernate-mapping>

数据库表:

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;

需要更新的代码是MVC控制器,如下所示:

  public ActionResult Create()
  {
      return View();
  }

  [HttpPost]
  public ActionResult Create(Comment comments)
  {
      try
      {
          using (ISession session = NHIbernateSession.OpenSession())
          {
              using (ITransaction transaction = session.BeginTransaction())
              {
                  session.Save(comments);
                  transaction.Commit();  
              }
          }
          return RedirectToAction("Index");
      }
      catch (Exception exception)
      {
          return View();
      }
  }

使用ASP.NET MVC和NHibernate更新外键

我没有看到将新的CommentPost关联的代码。

// Somehow get the target Post and store it the post variable, then
post.Comments.Add(comments)
comments.Post = post;
Session.Save(comments);
Session.Save(post);
Session.Commit();

像这样更改Post类

public class Post
{
    public virtual int Id { get; set; }
    [Display(Name = "Post")] 
    public virtual string PostText { get; set; }
    public virtual IList<Comment> Comments { get; protected set; }
    public Post()
    {
         Comments = new List<Comment>();
    }
    public virtual void AddComment(Comment comment)
    {
        if (!Comments.Contains(comment))
        {
             Comments.Add(comment);
             comment.Post = this;
        }
    }
}

在你的控制器中,使用会话获取帖子,并向其添加注释。