我的解决方案中有以下地图
public BlogMap() {
Table("Blogs");
LazyLoad();
Id(x => x.BlogId).GeneratedBy.Identity().Column("BlogId");
Map(x => x.BlogName).Column("BlogName").Not.Nullable();
Map(x => x.BlogCreateDate).Column("BlogCreateDate");
Map(x => x.PostCount).Column("PostCount");
HasMany(x => x.BlogPosts).KeyColumn("BlogId");
}
public BlogPostMap() {
Table("BlogPosts");
LazyLoad();
CompositeId().KeyProperty(x => x.PostId, "PostId").KeyProperty(x => x.BlogId,`enter code here` "BlogId");
References(x => x.Blog).Column("BlogId");
Map(x => x.PostText).Column("PostText");
Map(x => x.CreateDate).Column("CreateDate");
}
一个博客可能包含许多博客帖子
我正在尝试创建一个新的 BlogPost 并将其保存到数据库中。以下是我尝试执行的操作,但它不起作用。
BlogPost newbgPost = new BlogPost();
newbgPost.BlogId = currMasterBlogId;
newbgPost.CreateDate = DateTime.Now;
newbgPost.PostText = newPostText;
newbgPost.PostId = newPostId+1;
repBlogPost.Save(newbgPost);
References(x => x.Blog).Column("BlogId");
如果我在我的 BlogPost 地图中删除,上面的代码有效。
我知道新的 BlogPost 正在寻找 BlogPost 中的引用,我可以通过使用newbgPost.BlogId = currMasterBlogId;
基本上是我希望 BlogPost 引用的 BlogId 来做到这一点。
下面是 BlogPost 类
public class BlogPost {
public virtual int PostId { get; set; }
public virtual int BlogId { get; set; }
public virtual Blog Blog { get; set; }
public virtual string PostText { get; set; }
public virtual System.Nullable<System.DateTime> CreateDate { get; set; }
#region NHibernate Composite Key Requirements
public override bool Equals(object obj) {
if (obj == null) return false;
var t = obj as BlogPost;
if (t == null) return false;
if (PostId == t.PostId && BlogId == t.BlogId)
return true;
return false;
}
public override int GetHashCode() {
int hash = 13;
hash += PostId.GetHashCode();
hash += BlogId.GetHashCode();
return hash;
}
#endregion
}
博客类:
public class Blog {
public Blog() {
BlogPosts = new List<BlogPost>();
}
public virtual int BlogId { get; set; }
public virtual string BlogName { get; set; }
public virtual System.Nullable<System.DateTime> BlogCreateDate { get; set; }
public virtual System.Nullable<int> PostCount { get; set; }
public virtual IList<BlogPost> BlogPosts { get; set; }
}