public static RetailOutlet AddTenantToDatabaseWithSaveChanges(string name, TenantBase parent, CompanyDbContext context)
        {
            var newTenant = new RetailOutlet(name, parent);

            TenantBase.AddTenantToDatabaseWithSaveChanges(newTenant, context);
            return(newTenant);
        }
예제 #2
0
        public static SubGroup AddTenantToDatabaseWithSaveChanges(string name, TenantBase parent, CompanyDbContext context)
        {
            var newTenant = new SubGroup(name, parent);

            TenantBase.AddTenantToDatabaseWithSaveChanges(newTenant, context);
            return(newTenant);
        }
        public static Company AddTenantToDatabaseWithSaveChanges(string name, PaidForModules allowedPaidForModules,
                                                                 CompanyDbContext context)
        {
            var newTenant = new Company(name, allowedPaidForModules);

            TenantBase.AddTenantToDatabaseWithSaveChanges(newTenant, context);
            return(newTenant);
        }
        //----------------------------------------------------
        // public methods

        public void MoveToNewParent(TenantBase newParent, DbContext context)
        {
            void SetKeyExistingHierarchy(TenantBase existingTenant)
            {
                existingTenant.SetDataKeyFromHierarchy();
                if (existingTenant.Children == null)
                {
                    context.Entry(existingTenant).Collection(x => x.Children).Load();
                }

                if (!existingTenant._children.Any())
                {
                    return;
                }
                foreach (var tenant in existingTenant._children)
                {
                    SetKeyExistingHierarchy(tenant);
                }
            }

            if (this is Company)
            {
                throw new ApplicationException($"You cannot move a Company.");
            }
            if (newParent == null)
            {
                throw new ApplicationException($"The parent cannot be null.");
            }
            if (newParent.ParentItemId == 0)
            {
                throw new ApplicationException($"The parent {newParent.Name} must be already in the database.");
            }
            if (newParent == this)
            {
                throw new ApplicationException($"You can't be your own parent.");
            }
            if (context.Entry(this).State == EntityState.Detached)
            {
                throw new ApplicationException($"You can't use this method to add a new tenant.");
            }
            if (context.Entry(newParent).State == EntityState.Detached)
            {
                throw new ApplicationException($"The parent must already be in the database.");
            }

            Parent._children?.Remove(this);
            Parent = newParent;
            //Now change the data key for all the hierarchy from this entry down
            SetKeyExistingHierarchy(this);
        }
        protected static void AddTenantToDatabaseWithSaveChanges(TenantBase newTenant, CompanyDbContext context)
        {
            if (newTenant == null)
            {
                throw new ArgumentNullException(nameof(newTenant));
            }

            if (!(newTenant is Company))
            {
                if (newTenant.Parent == null)
                {
                    throw new ApplicationException($"The parent cannot be null in type {newTenant.GetType().Name}.");
                }
                if (newTenant.Parent.ParentItemId == 0)
                {
                    throw new ApplicationException($"The parent {newTenant.Parent.Name} must be already in the database.");
                }
            }
            if (context.Entry(newTenant).State != EntityState.Detached)
            {
                throw new ApplicationException($"You can't use this method to add a tenant that is already in the database.");
            }

            //We have to do this request using a transaction to make sure the DataKey is set properly
            using (var transaction = context.Database.BeginTransaction())
            {
                //set up the backward link (if Parent isn't null)
                newTenant.Parent?._children.Add(newTenant);
                context.Add(newTenant);  //also need to add it in case its the company
                // Add this to get primary key set
                context.SaveChanges();

                //Now we can set the DataKey
                newTenant.SetDataKeyFromHierarchy();
                context.SaveChanges();

                transaction.Commit();
            }
        }
 protected TenantBase(string name, TenantBase parent)
 {
     Name      = name;
     Parent    = parent;
     _children = new HashSet <TenantBase>();  //Used when creating a new version - not used by EF Core
 }
        }                                                  //Needed by EF Core

        private RetailOutlet(string name, TenantBase parent) : base(name, parent)
        {
        }
예제 #8
0
        }                                              //Needed by EF Core

        private SubGroup(string name, TenantBase parent) : base(name, parent)
        {
        }