views:

21

answers:

1

hi i have written a code like this

 public class person
        {
            public person()
            {
                 public virtual int Id { get; set; }
                 public virtual string Code { get; set; }
            } 
        }
        public class Child : Person
        {
            public person()
            {
                 public virtual string Name{ get; set; }
                 public virtual string Lastname{ get; set; }
            } 
        }
        public class Book
       {
           public virtual int Id { get; set; }
           public virtual string Name {get;set;}

       }

and my Mapper classes is like these

   public class PersonMapping : ClassMap<Person>
   {
            public PersonMapping()
            {
                Table("tblPersons");
                Id(x => x.Id).GeneratedBy.Native();
                Map(p => p.Code);

                JoinedSubClass<Child>("Id", MapChild);
            }

            public static void MapChild(JoinedSubClassPart<Child> child)
            {
                child.Table("tblChilds");
                child.Map(p => p.Name);
                child.Map(p => p.Lastname);
                child.HasMany(x => x.Relatives);
              }

    }
    public class RelativeMapping : ClassMap<Relative>
    {

       public RelativeMapping()
       {
          Table("tblRelatives");
          Id(x => x.Id);
          Map(p => p.Name);
          References(x => x.Child).Column("ChildId");
       }


}

this is Configuration

 Assembly assm = Assembly.Load("BLL");
                return   Fluently.Configure()
                        .Database(MsSqlConfiguration.MsSql2008
                        .ConnectionString(c => c
                        .FromAppSetting("ConnStr"))
                        .Cache(c => c
                        .UseQueryCache()
                        .ProviderClass<HashtableCacheProvider>())
                        .ShowSql())
                        .Mappings(m => m.FluentMappings.AddFromAssembly(assm))
                        .BuildSessionFactory();

and this is the code of delete

 public void Delete<T>(T obj)
        {
            ISessionFactory fact = FluentConfiguration.CreateSessionFactory();
            ISession session = fact.OpenSession();
            session.Delete(obj);
            session.Flush();

        }

my problem : when i wanna delete a Child it messages
" ILLEGAL ATTEMPT TO ASSOCIATE WITH TWO OPEN SESSION "

please help me

A: 

The error tells you the problem, and it is not with your entities or your mapping.

You have two or more open sessions and you're attempting to associate some entity with more than one of them.

update

In response to the updated code, I see that you have a method that accepts an entity as a parameter, creates a new session factory, creates a new session, and then tries to delete the entity.

There are some problems here:

  1. You should only create the session factory once. Ever. This is an expensive operation.
  2. You are passing the entity to the Delete() method. Where is this entity coming from? You've clearly already loaded it elsewhere in your application, using a different ISession. This is the crux of the problem. Unless you Evict() the entity from the first ISession (not recommended), trying to manipulate it with a different ISession will throw.
  3. You're calling Flush() which should almost never be used.
  4. You're using an implicit transaction.

You should really be deleting the entity with the same ISession with which it was loaded, and you should be performing work within a transaction, like this:

using(var transaction = session.BeginTransaction())
{
    session.Delete(obj);
    transaction.Commit();
}
Jay
hithanks for use answer but when i wanna add a new child it dosnt have this problemonly in deleting a child it has this problemwhat should i do with this?
@ushuah Potential answerers will need to see the pertinent code in order to assist you -- the code where you are creating session(s) and deleting the child entity.
Jay
i have edit the code
@ushuah I have edited the answer.
Jay

related questions