Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8142921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T12:57:15+00:00 2026-06-06T12:57:15+00:00

I have two entities like this: public class Service { public virtual int ServiceId

  • 0

I have two entities like this:

public class Service
{
    public virtual int ServiceId { get; set; }
    public virtual char LayoutCode { get; set; }
    public virtual string ServiceNameEn { get; set; }
    public virtual string ServiceNameTh { get; set; }
    public virtual string ServiceDescEn { get; set; }
    public virtual string ServiceDescTh { get; set; }
    public virtual string ServicePhone { get; set; }
    public virtual byte[] ServiceImage { get; set; }
    public virtual bool Permanent { get; set; }
    public virtual bool Active { get; set; }
    public virtual bool IsAutoAssign { get; set; }
    public virtual DateTime CreatedDatetime { get; set; }
    public virtual DateTime LastUpdatedDatetime { get; set; }
    public virtual string RedirectUrl { get; set; }
    public virtual IList<Reply> Replies { get; set; }
    public virtual IList<Employee> Employees { get; set; }
    public virtual IList<ServicesOfUser> ServicesOfUser { get; set; }
    private IList<Category> _categories = new List<Category>();
    public virtual IEnumerable<Category> Categories
    {
        get
        {
            return _categories;
        }
    }
    public virtual void Add(Category category)
    {
        if (!_categories.Any(x => x.CategoryId == category.CategoryId))
        {
            _categories.Add(category);
        }
    }
    public virtual void Clear()
    {
        _categories.Clear();
    }
}

and

public class Category
{
    public virtual int CategoryId { get; set; }
    public virtual string CategoryNameEn { get; set; }
    public virtual string CategoryNameTh { get; set; }
    public virtual string CategoryDescEn { get; set; }
    public virtual string CategoryDescTh { get; set; }
    public virtual byte[] CategoryImage { get; set; }
    public virtual bool Active { get; set; }
    public virtual DateTime CreatedDatetime { get; set; }
    public virtual DateTime LastUpdatedDatetime { get; set; }
    public virtual IList<Service> Services { get; set; }
}

The relationship of these two entities is many-to-many with mappings:

public class ServiceMap : ClassMap<Service>
{
    public ServiceMap()
    {
        Table("[SERVICES]");

        Id(x => x.ServiceId)
            .GeneratedBy.Identity()
            .Column("service_id")
            .CustomType("int")
            .Access.Property()
            .CustomSqlType("int")
            .Not.Nullable();
        Map(x => x.LayoutCode)
            .Column("layout_code")
            .CustomType("char")
            .Access.Property()
            .CustomSqlType("char")
            .Length(1)
            .Not.Nullable();
        Map(x => x.ServiceNameEn)
            .Column("service_name_en")
            .Access.Property()
            .CustomType("string")
            .CustomSqlType("varchar")
            .Length(128)
            .Not.Nullable();
        Map(x => x.ServiceNameTh)
            .Column("service_name_th")
            .Access.Property()
            .CustomType("string")
            .CustomSqlType("nvarchar")
            .Length(128)
            .Not.Nullable();
        Map(x => x.ServiceDescEn)
            .Column("service_desc_en")
            .CustomType("string")
            .Access.Property()
            .CustomSqlType("varchar")
            .Length(4000)
            .Nullable();
        Map(x => x.ServiceDescTh)
            .Column("service_desc_th")
            .CustomType("string")
            .Access.Property()
            .CustomSqlType("nvarchar")
            .Length(4000)
            .Nullable();
        Map(x => x.ServicePhone)
            .Column("service_phone")
            .Access.Property()
            .CustomType("string")
            .CustomSqlType("nvarchar")
            .Length(50)
            .Nullable();
        Map(x => x.ServiceImage)
            .Column("service_image")
            .CustomType("BinaryBlob")
            .Access.Property()
            .Generated.Never()
            .CustomSqlType("image")
            .Length(131231)
            .Nullable();
        Map(x => x.Permanent)
            .Column("permanent")
            .CustomType("bool")
            .Access.Property()
            .CustomSqlType("bit")
            .Not.Nullable();
        Map(x => x.Active)
            .Column("active")
            .CustomType("bool")
            .Access.Property()
            .CustomSqlType("bit")
            .Not.Nullable();
        Map(x => x.IsAutoAssign)
            .Column("is_auto_assign")
            .CustomType("bool")
            .Access.Property()
            .CustomSqlType("bit")
            .Not.Nullable();
        Map(x => x.CreatedDatetime)
            .Column("created_datetime")
            .Access.Property()
            .CustomType("DateTime")
            .CustomSqlType("datetime")
            .Not.Nullable();
        Map(x => x.LastUpdatedDatetime)
            .Column("last_updated_datetime")
            .Access.Property()
            .CustomType("DateTime")
            .CustomSqlType("datetime")
            .Not.Nullable();
        Map(x => x.RedirectUrl)
            .Column("redirect_url")
            .Access.Property()
            .CustomType("string")
            .CustomSqlType("nvarchar")
            .Length(213123)
            .Nullable();
        HasMany<ServicesOfUser>(x => x.ServicesOfUser)
            .AsBag()
            .Cascade.None()
            .LazyLoad()
            .Inverse()
            .KeyColumns.Add("service_id", mapping => mapping.Name("service_id")
                .SqlType("int")
                .Nullable());
        HasMany<Reply>(x => x.Replies)
            .AsBag()
            .Cascade.None()
            .LazyLoad()
            .Inverse()
            .KeyColumns.Add("send_to_service_id", mapping => mapping.Name("send_to_service_id")
                .SqlType("int")
                .Nullable());
        HasManyToMany<Employee>(x => x.Employees)
            .AsBag()
            .Cascade.None()
            .LazyLoad()
            .Table("SERVICE_MONITORS")
            .Inverse()
            .ChildKeyColumns.Add("employee_id", mapping => mapping.Name("employee_id")
                .SqlType("uniqueidentifier")
                .Not.Nullable())
            .ParentKeyColumns.Add("service_id", mapping => mapping.Name("service_id")
                .SqlType("int")
                .Not.Nullable());
        HasManyToMany<Category>(x => x.Categories)
            .AsBag()
            .Access.CamelCaseField(Prefix.Underscore)
            .Cascade.AllDeleteOrphan()
            .Table("SERVICES_IN_CATEGORIES")
            .ChildKeyColumns.Add("category_id", mapping => mapping.Name("category_id")
                .SqlType("int")
                .Not.Nullable())
            .ParentKeyColumns.Add("service_id", mapping => mapping.Name("service_id")
                .SqlType("int")
                .Not.Nullable());
    }
}

And

public class CategoryMap : ClassMap<Category>
{
    public CategoryMap()
    {
        Table("[SERVICE_CATEGORIES]");

        Id(x => x.CategoryId)
            .GeneratedBy.Identity()
            .Column("category_id")
            .CustomType("int")
            .Access.Property()
            .CustomSqlType("int")
            .Not.Nullable();
        Map(x => x.CategoryNameEn)
            .Column("category_name_en")
            .CustomType("string")
            .Access.Property()
            .CustomSqlType("varchar")
            .Length(128)
            .Not.Nullable();
        Map(x => x.CategoryNameTh)
            .Column("category_name_th")
            .CustomType("string")
            .Access.Property()
            .CustomSqlType("nvarchar")
            .Length(128)
            .Not.Nullable();
        Map(x => x.CategoryDescEn)
            .Column("category_desc_en")
            .CustomType("string")
            .Access.Property()
            .CustomSqlType("varchar")
            .Length(3423423)
            .Not.Nullable();
        Map(x => x.CategoryDescTh)
            .Column("category_desc_th")
            .CustomType("string")
            .Access.Property()
            .CustomSqlType("nvarchar")
            .Length(3243234)
            .Not.Nullable();
        Map(x => x.CategoryImage)
            .Column("category_image")
            .CustomType("BinaryBlob")
            .Access.Property()
            .Generated.Never()
            .CustomSqlType("image")
            .Length(131231)
            .Nullable();
        Map(x => x.Active)
            .Column("active")
            .CustomType("bool")
            .Access.Property()
            .CustomSqlType("bit")
            .Not.Nullable();
        Map(x => x.CreatedDatetime)
            .Column("created_datetime")
            .Access.Property()
            .CustomType("DateTime")
            .CustomSqlType("datetime")
            .Not.Nullable();
        Map(x => x.LastUpdatedDatetime)
            .Column("last_updated_datetime")
            .Access.Property()
            .CustomType("DateTime")
            .CustomSqlType("datetime")
            .Not.Nullable();
        HasManyToMany<Service>(x => x.Services)
            .AsBag()
            .Cascade.AllDeleteOrphan()
            .Inverse()
            .LazyLoad()
            .Table("SERVICES_IN_CATEGORIES")
            .ChildKeyColumns.Add("service_id", mapping => mapping.Name("service_id")
                .SqlType("int")
                .Not.Nullable())
            .ParentKeyColumns.Add("category_id", mapping => mapping.Name("category_id")
                .SqlType("int")
                .Not.Nullable());
    }
}

Coding:

using (unitOfWork = new UnitOfWork(SessionFactory))
{
    serviceRepo = new ServiceRepo(unitOfWork.Session);
    try
    {
        Service service = new Service();
        if (id != null)
            service = serviceRepo.GetById((int)id);

        // set values for service here

        IList<Category> categories = new List<Category>();
        // add some categories to service

        serviceRepo.Save(service);
        unitOfWork.Commit();

        return service;
    }
    catch (Exception ex)
    {
        unitOfWork.Rollback();
        return null;
    }
}

Service is updated, but the association records in join table aren’t deleted.

If I add this code below before adding categories to service:

service.Clear();

When commit, I get this error message:

not-null property references a null or transient value ilu.src.Entities.Category.CategoryDescEn

Full stack trace:

   at NHibernate.Engine.Nullability.CheckNullability(Object[] values, IEntityPersister persister, Boolean isUpdate)
   at NHibernate.Event.Default.DefaultDeleteEventListener.DeleteEntity(IEventSource session, Object entity, EntityEntry entityEntry, Boolean isCascadeDeleteEnabled, IEntityPersister persister, ISet transientEntities)
   at NHibernate.Event.Default.DefaultDeleteEventListener.OnDelete(DeleteEvent event, ISet transientEntities)
   at NHibernate.Impl.SessionImpl.FireDelete(DeleteEvent event, ISet transientEntities)
   at NHibernate.Impl.SessionImpl.Delete(String entityName, Object child, Boolean isCascadeDeleteEnabled, ISet transientEntities)
   at NHibernate.Engine.CascadingAction.DeleteCascadingAction.Cascade(IEventSource session, Object child, String entityName, Object anything, Boolean isCascadeDeleteEnabled)
   at NHibernate.Engine.Cascade.CascadeToOne(Object parent, Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled)
   at NHibernate.Engine.Cascade.CascadeAssociation(Object parent, Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled)
   at NHibernate.Engine.Cascade.CascadeProperty(Object parent, Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled)
   at NHibernate.Engine.Cascade.CascadeCollectionElements(Object parent, Object child, CollectionType collectionType, CascadeStyle style, IType elemType, Object anything, Boolean isCascadeDeleteEnabled)
   at NHibernate.Engine.Cascade.CascadeCollection(Object parent, Object child, CascadeStyle style, Object anything, CollectionType type)
   at NHibernate.Engine.Cascade.CascadeAssociation(Object parent, Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled)
   at NHibernate.Engine.Cascade.CascadeProperty(Object parent, Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled)
   at NHibernate.Engine.Cascade.CascadeOn(IEntityPersister persister, Object parent, Object anything)
   at NHibernate.Event.Default.DefaultDeleteEventListener.CascadeBeforeDelete(IEventSource session, IEntityPersister persister, Object entity, EntityEntry entityEntry, ISet transientEntities)
   at NHibernate.Event.Default.DefaultDeleteEventListener.DeleteEntity(IEventSource session, Object entity, EntityEntry entityEntry, Boolean isCascadeDeleteEnabled, IEntityPersister persister, ISet transientEntities)
   at NHibernate.Event.Default.DefaultDeleteEventListener.OnDelete(DeleteEvent event, ISet transientEntities)
   at NHibernate.Impl.SessionImpl.FireDelete(DeleteEvent event, ISet transientEntities)
   at NHibernate.Impl.SessionImpl.Delete(String entityName, Object child, Boolean isCascadeDeleteEnabled, ISet transientEntities)
   at NHibernate.Engine.CascadingAction.DeleteCascadingAction.Cascade(IEventSource session, Object child, String entityName, Object anything, Boolean isCascadeDeleteEnabled)
   at NHibernate.Engine.Cascade.CascadeToOne(Object parent, Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled)
   at NHibernate.Engine.Cascade.CascadeAssociation(Object parent, Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled)
   at NHibernate.Engine.Cascade.CascadeProperty(Object parent, Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled)
   at NHibernate.Engine.Cascade.CascadeCollectionElements(Object parent, Object child, CollectionType collectionType, CascadeStyle style, IType elemType, Object anything, Boolean isCascadeDeleteEnabled)
   at NHibernate.Engine.Cascade.CascadeCollection(Object parent, Object child, CascadeStyle style, Object anything, CollectionType type)
   at NHibernate.Engine.Cascade.CascadeAssociation(Object parent, Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled)
   at NHibernate.Engine.Cascade.CascadeProperty(Object parent, Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled)
   at NHibernate.Engine.Cascade.CascadeOn(IEntityPersister persister, Object parent, Object anything)
   at NHibernate.Event.Default.DefaultDeleteEventListener.CascadeBeforeDelete(IEventSource session, IEntityPersister persister, Object entity, EntityEntry entityEntry, ISet transientEntities)
   at NHibernate.Event.Default.DefaultDeleteEventListener.DeleteEntity(IEventSource session, Object entity, EntityEntry entityEntry, Boolean isCascadeDeleteEnabled, IEntityPersister persister, ISet transientEntities)
   at NHibernate.Event.Default.DefaultDeleteEventListener.OnDelete(DeleteEvent event, ISet transientEntities)
   at NHibernate.Impl.SessionImpl.FireDelete(DeleteEvent event, ISet transientEntities)
   at NHibernate.Impl.SessionImpl.Delete(String entityName, Object child, Boolean isCascadeDeleteEnabled, ISet transientEntities)
   at NHibernate.Engine.Cascade.DeleteOrphans(String entityName, IPersistentCollection pc)
   at NHibernate.Engine.Cascade.CascadeCollectionElements(Object parent, Object child, CollectionType collectionType, CascadeStyle style, IType elemType, Object anything, Boolean isCascadeDeleteEnabled)
   at NHibernate.Engine.Cascade.CascadeCollection(Object parent, Object child, CascadeStyle style, Object anything, CollectionType type)
   at NHibernate.Engine.Cascade.CascadeAssociation(Object parent, Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled)
   at NHibernate.Engine.Cascade.CascadeProperty(Object parent, Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled)
   at NHibernate.Engine.Cascade.CascadeOn(IEntityPersister persister, Object parent, Object anything)
   at NHibernate.Event.Default.AbstractFlushingEventListener.CascadeOnFlush(IEventSource session, IEntityPersister persister, Object key, Object anything)
   at NHibernate.Event.Default.AbstractFlushingEventListener.PrepareEntityFlushes(IEventSource session)
   at NHibernate.Event.Default.AbstractFlushingEventListener.FlushEverythingToExecutions(FlushEvent event)
   at NHibernate.Event.Default.DefaultFlushEventListener.OnFlush(FlushEvent event)
   at NHibernate.Impl.SessionImpl.Flush()
   at NHibernate.Transaction.AdoTransaction.Commit()
   at ilu.src.Common.UnitOfWork.Commit() in D:\Arunsawad\iLertU\ilu\ilu\src\Common\UnitOfWork.cs:line 41
   at ilu.Controllers.AdminController.SaveService(Nullable`1 id, String name, String nameth, String desc, String descth, String phone, String catids, String url, Char layoutcode, Boolean permanent, Boolean active, Boolean autoassign) in D:\Arunsawad\iLertU\ilu\ilu\Controllers\AdminController.cs:line 107

Is there anything I’m missing here?

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T12:57:17+00:00Added an answer on June 6, 2026 at 12:57 pm

    Declare cascades as Cascade.AllDeleteOrphan(). Also, try adding .Inverse() to one of the many-to-many mappings.

    Some more information:

    Bidirectional associations from NHibernate documentation
    NHibernate Pitfalls: Many to Many and Inverse
    NHibernate Cascades: the different between all, all-delete-orphans and save-update


    Edit:

    Judging by the error you got, you just need to initialize CategoryDescEn property of the category before trying to save it. Seems like it was null when you tried to save:

    not-null property references a null or transient value
    ilu.src.Entities.Category.CategoryDescEn

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have two Entities. public class Category{ public virtual int Id{get;set;} public virtual
I have two entities like: public class Employee { public int Id { get;
I have two entities: public class Parent() { public ICollection<Child> Children { get; set;
I have two entities like the following: @Entity public class Trip { @OneToMany(mappedBy =
I have an entity like this: public class Employment { public virtual Company Company
I have two simplest of Entities, similar to: public class User { int id;
I have two Doctrine entities that have a one-to-many relationship, like this: License class
Supposed I have two classes like this: public class Parent { public string Id
I have the following two entities: public class Field { public int FieldID {
A have two entities. For example timing settings and orders. @Entity public class TimingSettings{

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.