0

I am getting this error while I am converting.

Initializing[Entity.Tag#8]-failed to lazily initialize a collection of role: Entity.Tag.Feed, no session or session was closed

Feed feed = new Feed();

Tag tag = Tag.READ.ById(8);
feed.Tag.Add(tag);
feed.Language = ENLanguage.EN;
feed.Name = "Foo";

feed.Save();

string x = JsonConvert.SerializeObject(feed);

Feed Class

public class Feed : BaseClass<Feed>
{
    public virtual int FeedId { get; set; }
    public virtual IList<Tag> Tag { get; set; }
    public virtual ENLanguage Language { get; set; }
    public virtual string Name { get; set; }

    public Feed()
    {
        Tag = new List<Tag>();
    }

    public virtual void AddTag(Tag tag)
    {
        tag.Feed.Add(this);
        Tag.Add(tag);
    }
}

Feed Map

public class FeedMap : ClassMap<Feed>
{
    public FeedMap()
    {
        Id(x => x.FeedId);
        HasManyToMany(x => x.Tag).Cascade.All().Table("FeedTag");
        Map(x => x.Language).CustomType<short>();
        Map(x => x.Name);
    }
}

Tag Class

public class Tag : BaseClass<Tag>
{
    public virtual int TagId { get; set; }
    public virtual IList<Feed> Feed { get; protected set; }
    public virtual string Name { get; set; }
}

Tag Map

public class TagMap : ClassMap<Tag>
{
    public TagMap()
    {
        Id(x => x.TagId);
        Map(x => x.Name);
        HasManyToMany(x => x.Feed).Cascade.All().Inverse().Table("FeedTag");
    }
}

Save Method

    using (var session = NHibernateHelper.OpenSession())
    {
        using (var transaction = session.BeginTransaction())
        {
            session.SaveOrUpdate(x);

            transaction.Commit();
        }
    }
10
  • Where's you session starting and ending? I've had that problem before when I've loaded something in the controller, but when the view trys to do something with it, the session is closed. Commented Sep 14, 2014 at 20:34
  • I think you are asking for save method? I added to question. Commented Sep 14, 2014 at 20:37
  • oh! you are closing the session straight away..... that means it can't lazy load things later Commented Sep 14, 2014 at 20:42
  • I am not familiar with nhibernate, what should be the right way to do? Commented Sep 14, 2014 at 20:44
  • are you doing a web app? or something else? Commented Sep 14, 2014 at 21:18

2 Answers 2

2

Sounds like your session is ended too quickly. You only open it for your save.

try

using (var session = NHibernateHelper.OpenSession())
{
  Feed feed = new Feed();

  Tag tag = Tag.READ.ById(8);
  feed.Tag.Add(tag);
  feed.Language = ENLanguage.EN;
  feed.Name = "Foo";

  feed.Save();

  string x = JsonConvert.SerializeObject(feed);
}
Sign up to request clarification or add additional context in comments.

2 Comments

What if I only cover SerializeObject with using? does that work? If It is I am planing to create ToJson generic method which handles sessin inside it. so I could use it like "feed.ToJson()"
[JsonIgnore] is solved my problem but I learned something new from you guys! appreciated!
1

I think your problem that you don't initialize lazy collection IList Feed of class Tag when you get object from database

Tag.READ.ById(8); // in that method you have to initialize Feed

something like

using(session = NHibernateHelper.OpenSession())
{
var TagObjectFromDb = session.Get<Tag>(id);
NHibernateUtil.Initialize(TagObjectFromDb.Feed);//initialize lazy collection, 
return TagObjectFromDb;
}

and

Feed fbfeed = new Feed();//fbfeed - typo?

Tag tag = Tag.READ.ById(8);
feed.Tag.Add(tag);//fbfeed - typo?

4 Comments

READ.ById is a generic method. What you are suggesting is not suitable. any way around?
when you call string x = JsonConvert.SerializeObject(feed); instance feed must by fully initialized. For collection properties like IList<Feed> Feed or IList<Tag> Tag you have to call NHibernateUtil.Initialize() inside opened session
In your case i can think of simple solution,since you don't have opened session, which you could use to initialize collection. Or maybe you have but i can't see that in your example.
[JsonIgnore] is solved my problem but I learned something new from you guys! appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.