With Fluent Nhibernate update parent and children

170 views
Skip to first unread message

ho ihu

unread,
Feb 12, 2022, 6:00:24 PM2/12/22
to nhusers
Dear all, 
is there any one to help me with update problem of fluent Nhibernate. I want to update parent and its children. 

problem: it is not updating but inserting new rows in database. My code is as below
I will be grateful for your helping.

Mappings: Parent

    public class ProjectsMapping : ClassMap<Project>
    {
        public ProjectsMapping()
        {
            Table("Projects");
            Id(u => u.PId).GeneratedBy.Identity();
            Map(u => u.Id).Column("ApiProjectId");
            Map(u => u.Name);
            Map(u => u.EditorGuid);
            Map(u => u.CreatedOn);
            Map(u => u.CreatedBy);
            Map(u => u.ModifiedOn);
            Map(u => u.ModifiedBy);
            HasMany(u => u.Roles).KeyColumn("RoleProjectId").Inverse().Cascade.AllDeleteOrphan();
         
        }
Mapping: children
    public class RoleMapping : ClassMap<Role>
    {
        public RoleMapping()
        {
            Table("ModuleRole");
            Id(u => u.RId).GeneratedBy.Identity();
            Map(u => u.Name);
            Map(u => u.Site_Id).Column("SiteId");
            Map(u => u.Site_Name).Column("SiteName");
            Map(u => u.Domain);
            Map(u => u.EditorGuid);
            References(x => x.Project).Column("RoleProjectId").Cascade.SaveUpdate().Not.LazyLoad();
        }
    }

updating code:

        public void Update(T entities)
        {
            using (ISession _session = DatabaseContext.SessionOpen())
            {
                using (ITransaction _transaction = _session.BeginTransaction())
                    try
                    {
                        _session.Update(entities);  //I tried  SaveOrUpdate instead of Update
                        _session.Flush();
                        _transaction.Commit();

                    }
                    catch (Exception ex)
                    {
                        if (!_transaction.WasCommitted)
                        {
                            _transaction.Rollback();
                        }

                    }
            }
        }
Reply all
Reply to author
Forward
0 new messages