Error while updating Entity (ActiveRecord)

22 views
Skip to first unread message

Pbirkoff

unread,
Oct 22, 2012, 5:16:42 PM10/22/12
to castle-pro...@googlegroups.com
I've been wasting about 4 hours now, trying to solve a really weird problem with Active Record. I'm building a forum, and I want to update the Forum-entity, when a new ForumPost is being saved, so the number of posts are correct. There are my classes (i've simplfied them a bit, just the important stuff

[ActiveRecord(Lazy = true)]
    public class Forum
    {
        [PrimaryKey(Generator = PrimaryKeyType.Identity)]
        public virtual int Id { get; set; }

        [Property]
        public virtual int NumberOfPosts { get; set; }

        [HasMany(typeof(ForumThread), Lazy = true, Inverse = true)]
        public virtual IList<ForumThread> Threads { get; set; }
}

[ActiveRecord(Lazy = true)]
public class ForumThread
{
    [PrimaryKey(Generator = PrimaryKeyType.Identity)]
    public virtual int Id { get; set; }

    [Property]
    public virtual string Title { get; set; }

    [BelongsTo]
    public virtual Forum Forum { get; set; }
}

Somewhere in my program, is the following code (also simplified)

var thread = ActiveRecordMediator<ForumThread>.FindByPrimaryKey(id);
var forum = thread.Forum;

forum.NumberOfCodes = 9; //This value is being calculated

ActiveRecordMediator<Forum>.Save(forum);

To me, this looks very normal, but somehow the following error occurs:

You have accessed an ActiveRecord class that wasn't properly initialized. There are two possible explanations: that the call to ActiveRecordStarter.Initialize() didn't include ForumProxy class, or that ForumProxy class is not decorated with the [ActiveRecord] attribute.


Ehm, WHY? I really have now idea how this is possible. All the entities are properly initialized in the Global.asax (a lot of other things are working correct). I assume the Forum is Lazy loaded, and therefor a Proxy, but why doesn't the program know it's a proxy? How am I suppose to make the code work? I know setting Forum as Lazy = false works, but I don't want that..

Hope someone can help. I'm using all the latest versions, loaded via NuGet.

Reply all
Reply to author
Forward
0 new messages