Laurent,

In your Blog class, you need:

[HasMany(typeof(Postc), Table = "Posts", ColumnKey = "post_blogid")]

Instead of:

[HasMany(typeof(Postc))]

In your Post class, you need to remove:

        [BelongsTo("post_blogid")]
        public Blog Blog
        {
            get { return _blog; }
            set { _blog = value; }
        }

Composite keys are... not fun.

-Michael Maddox
http://www.AgilityForORMs.com/Home/Products/AfoCastleActiveRecordModelCodeGenerator/

On Fri, Sep 10, 2010 at 3:29 AM, Laurent Albarède
<[email protected]> wrote:
> Hi,
> I'm a new user of ActiveRecord, changing my ORM from Gentle to
> NHibernate because of client request and upgrade to an alive ORM.
> In my database, I've got many foreign keys and composite keys.
> To do simple, I will describe my problem with Blog and Posts sample :
>
> -----------------------------------------------------------------------------------------------------------------------
> Database
> -----------------------------------------------------------------------------------------------------------------------
> Blog contains fields :
> blog_id (PrimaryKey, with a sequence)
> blog_name
>
> Post contains fields :
> post_title
> post_blogid
>
> post_blogid is a foreign key to Blog.blog_id.
> post_title and post_blogid are the composite Primarykey.
>
> Well, now I've made class in my .Net 3.5 project :
>
> A Blog class, which contains :
> -the primary key with sequence (this is working)
> -an IList of posts
> -a relation HasMany to posts
>
> A Postc Class which contains :
> - A compositekey mapped to attributes in database
> - my foreign key with BelongsTo attribute
> -a constructeur which sets the key properly (I think ?...)
>
> (I have also a Post class with a simple primarykey (post_id) with a
> sequence. In this case, everythin seems to work.)
>
> Now I do :
>            Blog b = new Blog("MyBlog");
>            b.Create();
>            Postc p = new Postc(b, "myfirstpost");
>            p.Create();
>
> And I obtain this error :
> ArgumentOutOfRangeException
> Index out of limits (translation from french, not the exact message).
>   à
> System.ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument
> argument, ExceptionResource resource)
>   à System.ThrowHelper.ThrowArgumentOutOfRangeException()
>   à System.Collections.Generic.List`1.get_Item(Int32 index)
>   à Npgsql.NpgsqlParameterCollection.get_Item(Int32 index) dans C:
> \projects\Npgsql2\src\Npgsql\NpgsqlParameterCollection.cs:ligne 117
>   à Npgsql.NpgsqlParameterCollection.GetParameter(Int32 index) dans C:
> \projects\Npgsql2\src\Npgsql\NpgsqlParameterCollection.cs:ligne 494
>   à
> System.Data.Common.DbParameterCollection.System.Collections.IList.get_Item(Int32
> index)
>   à NHibernate.Type.Int32Type.Set(IDbCommand rs, Object value, Int32
> index)
>   à NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object
> value, Int32 index)
>   à NHibernate.Type.NullableType.NullSafeSet(IDbCommand st, Object
> value, Int32 index, ISessionImplementor session)
>   à NHibernate.Type.ComponentType.NullSafeSet(IDbCommand st, Object
> value, Int32 begin, ISessionImplementor session)
>   à
> NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object
> id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[]
> [] includeColumns, Int32 table, IDbCommand statement,
> ISessionImplementor session, Int32 index)
>   à NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object
> id, Object[] fields, Boolean[] notNull, Int32 j, SqlCommandInfo sql,
> Object obj, ISessionImplementor session)
>
>
>
>
> If someone have an idea of what's happening... thank you very much!
>
>
> -----------------------------------------------------------------------------------------------------------------------
> Blog Class
> -----------------------------------------------------------------------------------------------------------------------
>    [ActiveRecord("Blogs")]
>    public class Blog : ActiveRecordBase
>    {
>        private int _id;
>        private String _name;
>        private IList _posts;
>
>        public Blog()
>        {
>        }
>
>        public Blog(String name)
>        {
>            _name = name;
>        }
>
>
> [PrimaryKey(PrimaryKeyType.Sequence,"blog_id",SequenceName=("blogs_blog_id_seq"))]
>        public int Id
>        {
>            get { return _id; }
>            set { _id = value; }
>        }
>
>        [Property("blog_name")]
>        public String Name
>        {
>            get { return _name; }
>            set { _name = value; }
>        }
>
>
>        [HasMany(typeof(Postc))]
>        public IList Posts
>        {
>            get { return _posts; }
>            set { _posts = value; }
>        }
>
>        public static void DeleteAll()
>        {
>            DeleteAll(typeof(Blog));
>        }
>
>        public static Blog[] FindAll()
>        {
>            return (Blog[])FindAll(typeof(Blog));
>        }
>
>        public static Blog Find(int id)
>        {
>            return (Blog)FindByPrimaryKey(typeof(Blog), id);
>        }
>    }
>
>
> -----------------------------------------------------------------------------------------------------------------------
> Posts Class
> -----------------------------------------------------------------------------------------------------------------------
>   [ActiveRecord("Posts")]
>    public class Postc : ActiveRecordBase
>    {
>        private PostKey _key;
>        private Blog _blog;
>
>        public Postc()
>        {
>        }
>
>        public Postc(Blog blog, String title)
>            : this()
>        {
>            _blog = blog;
>            _key = new PostKey(title, blog.Id);
>
>        }
>
>
>        [CompositeKey]
>        public PostKey Key
>        {
>            get { return _key; }
>            set { _key = value; }
>        }
>
>
>        [BelongsTo("post_blogid")]
>        public Blog Blog
>        {
>            get { return _blog; }
>            set { _blog = value; }
>        }
>
>    }
>
>    [Serializable]
>    public class PostKey
>    {
>        private string _title;
>        private int _blogid;
>
>        [KeyProperty(Column="post_title")]
>        public string Title
>        {
>            get { return _title; }
>            set { _title = value; }
>        }
>
>        [KeyProperty(Column = "post_blogid")]
>        public int BlogId
>        {
>            get { return _blogid; }
>            set { _blogid = value; }
>        }
>
>        public PostKey()
>        {
>        }
>
>        public PostKey(string title,int blogid)
>        {
>            _title = title;
>            _blogid = blogid;
>        }
>
>        public override int GetHashCode()
>        {
>            return _title.GetHashCode() ^ _blogid;
>        }
>
>        public override bool Equals(object obj)
>        {
>            if (this == obj)
>            {
>                return true;
>            }
>            PostKey key = obj as PostKey;
>            if (key == null)
>            {
>                return false;
>            }
>            if (_title != key.Title || _blogid != key.BlogId)
>            {
>                return false;
>            }
>            return true;
>        }
>    }
>
> --
> 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.
>
>

-- 
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