Hi, thanks for that. I was stumbling over the example given in the
documentation at the bottom of the page here
http://www.castleproject.org/activerecord/documentation/trunk/usersguide/relations/hasmany.html

On Dec 20, 12:39 pm, Markus Zywitza <[email protected]> wrote:
> Short answer: you can't do that without inverse.
>
> Long answer:
>
> When you add a new post to a blog and save the blog, the post doesn't
> exist on the DB in that moment, so NH will save it. It will simply
> take the post object and insert its contents in the DB and since the
> Blog reference is null, exactly this will be inserted. Then the blog
> is saved. NH inspects the Posts collection and updates all objects
> there with the blog that is currently saved. So the blog reference on
> post will be null, but only temporary. After saving is complete, the
> FK in't null anymore.
>
> If you use inverse=true, the inverse collection of the blog will not
> be processed. You have to set post.Blog explicitly before saving and
> therefore no null value will be inserted in the table.
>
> If you can't use inverse=true, you'll have to add a method to add a
> member to that collection:
>
> public void AddPost(Post post)
> {
>   post.Blog = this;
>   posts.Add(post);
>
> }
>
> -Markus
>
> 2009/12/17 pnewhook <[email protected]>:
>
> > Hello,
>
> > I've been following the tutorial to make the simple blog. I'm creating
> > mine in ASP.Net, but I've been having trouble creating a post that
> > references a Blog instance where I don't use Inverse=true
>
> > part of my blog class
>
> >    [ActiveRecord]
> >    public class Blog : ActiveRecordBase<Blog>
> >    {
> >        private int id;
> >        private string name;
> >        private string author;
> >        private IList<Post> posts = new List<Post>();
>
> >        public Blog()
> >        {}
>
> >        public Blog(string name, string author)
> >        {
> >            this.name = name;
> >            this.author = author;
> >        }
>
> >        [HasMany]
> >        public IList<Post> Posts
> >        {
> >            get { return posts; }
> >            set { posts = value; }
> >        }
>
> >        [PrimaryKey (PrimaryKeyType.Identity)]
> >        public int Id
> >        {
> >            get {return id;}
> >            set { this.id = value; }
> >         }
>
> > part of my post class
>
> >    [ActiveRecord]
> >    public class Post : ActiveRecordBase<Post>
> >    {
> >        private int id;
> >        private String title;
> >        private String contents;
> >        private String category;
> >        private DateTime created;
> >        private int published;
> >        private Blog blog;
>
> >        public Post()
> >        {
> >            created = DateTime.Now;
> >        }
>
> >        [BelongsTo("BlogId")]
> >        public Blog Blog
> >        {
> >            get{return blog;}
> >            set {this.blog=value;}
> >        }
>
> >        [PrimaryKey(PrimaryKeyType.Identity)]
> >        public int Id
> >        {
> >            get { return id; }
> >            set { id = value; }
> >        }
>
> > and the default.aspx page that actually does something.
> > public partial class _Default : System.Web.UI.Page
> > {
> >    protected void Page_Load(object sender, EventArgs e)
> >    {
> >        Person myperson = new Person("name", "password");
> >        myperson.Create();
> >        Debug.Write("Created Person \n");
>
> >        Blog myBlog = new Blog();
> >        myBlog.Author = "non inverse";
> >        myBlog.Name = "non inverse";
> >        myBlog.Create();
> >        System.Diagnostics.Debug.Write("Blog Created \n");
>
> >        Post myPost = new Post();
> >        myPost.Contents = "non inverse";
> >        myPost.Title = "non inverse";
> >        myPost.Category = "Stuff";
>
> >        myPost.Published = 1;
> >        myPost.Create();
>
> >        myBlog.Posts.Add(myPost);
> >        myBlog.Update();
>
> >        System.Diagnostics.Debug.Write("Post Created \n");
>
> >    }
> > }
>
> > I get an error on myPost.Create() because it can't insert a null value
> > into BlogId. However, I tried calling myBlog.Posts.Add(myPost); before
> > creating the post and all the values were still null. What order
> > should I do this in.
>
> > Everything works for me when I use inverse=true.
>
> > Thanks for your help. This is my first ORM and I really like it.
>
> > --
>
> > You received this message because you are subscribed to the Google Groups 
> > "Castle Project Users" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to 
> > [email protected].
> > For more options, visit this group 
> > athttp://groups.google.com/group/castle-project-users?hl=en.

--

You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en.


Reply via email to