使用ASP.NET MVC和NHibernate更新外键 [英] Update foreign key using ASP.NET MVC and NHibernate
问题描述
我的应用程序中有一个child,parent关系,例如一个帖子可以有很多评论(一对多关系).我已经完成了映射的代码,我需要编写代码,因此,当我将一个孩子插入数据库时,它也会插入父ID.目前我的代码未插入父母ID,仅插入了孩子的其他详细信息.有人可以帮我取消我的代码吗?
I have a child , parent relationship in my application e.g.One Post can have many Comments(One to Many relationship). Ive completed the code for mapping, I need to write a code so when I insert a child into DB it inserts the parent ID too. At the moment my code doesnt insert the parent id only inserts other details of child.Can someone help me unpdating my code please?
型号:
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映射:
NHibenate mappings:
<?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控制器,如下所示:
The code that needs updating is the MVC controller which is as follows:
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();
}
}
推荐答案
我看不到将新Comment
关联到Post
的代码.
I don't see the code where you associate your new Comment
s to your Post
.
// 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();
这篇关于使用ASP.NET MVC和NHibernate更新外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!