Fluent Nhibernate to save data into multaple referenced tables,

8 views
Skip to first unread message

ho ihu

unread,
Jan 4, 2022, 3:19:59 PMJan 4
to nhusers
   
Dear Friends
I am trying to insert into multiple Projects into project table which has reference to roles and roles have reference to permissions. there are multiple projects, each project has multiple roles and each role has multiple permissions.

Here is my code as below. can you recorrect it. righ now it is not saveing entities. by save(entities) method of fluent nhibernate.

        public void Add(T entities)
        {
            using (ISession _session = DatabaseContext.SessionOpen())
            {
                using (ITransaction _transaction = _session.BeginTransaction())
                    try
                    {
                        _session.Save(entities);
                        _transaction.Commit();

                    }
                    catch (Exception ex)
                    {
                        if (!_transaction.WasCommitted)
                        {
                            _transaction.Rollback();
                        }
                        throw new Exception("Not Added Message: " + ex.Message + "Stack Trace: " + ex.StackTrace);
                    }
            }
        }

       
            public class Permission
    {
        public virtual int MId { get; set; }
        public virtual string Id { get; set; }
        public virtual bool View { get; set; }
        public virtual bool Add { get; set; }
        public virtual bool Edit { get; set; }
        public virtual bool Delete { get; set; }
        public virtual Role Roles { get; set; }
    }

    public class Role
    {
        public virtual int RId { get; set; }
        public virtual int Id { get; set; }
        public virtual string Name { get; set; }
        public virtual string Site_Id { get; set; }
        public virtual string Site_Name { get; set; }
        public virtual string Domain { get; set; }
        public virtual List<Permission> permissions { get; set; }
        public virtual Project Project { get; set; }
    }

    public class Project
    {
        public virtual int PId { get; set; }
        public virtual string Id { get; set; }
        public virtual string Name { get; set; }
        public virtual List<Role> Roles { get; set; }
    }
    public class Projects
    {
        public virtual List<Project> projects { get; set; }
    }
       
       
           public class PermissionMapping : ClassMap<Permission>
    {
        public PermissionMapping()
        {
            Table("ModulePermissions");
            Id(u => u.MId).GeneratedBy.Identity();
            Map(u => u.Id, "ModuleName");
            Map(u => u.View);
            Map(u => u.Edit);
            Map(u => u.Add);
            Map(u => u.Delete);
            References(x => x.Roles).Column("RolePermissionId");
        }
    }
       
            public class ProjectService : IProjectService
    {
        private readonly RepositoryBase<Project> _repository = new RepositoryBase<Project>();

        public void ProjectInserted(Project project)
        {
            _repository.Add(project);
        }

        public IList<Project> ProjectList()
        {
            return _repository.GetList();
        }
    }
       
            public class RoleService : IRoleService
    {
        private readonly RepositoryBase<Role> _repository = new RepositoryBase<Role>();

        public void RoleInserted(Role role)
        {
            _repository.Add(role);
        }

        public IList<Role> RoleList()
        {
            return _repository.GetList();
        }
    }

with kind regards

Reply all
Reply to author
Forward
0 new messages