Beispiel #1
0
        // Add a new domain handle:
        //   name, type, parentNameOrID  should be filled
        //   if dbName is not given, i.e., null, default database name will be used.
        //
        public static async Task <iS3DomainHandle> AddDomainHandle(string name,
                                                                   string type, string parentNameOrID, string dbName = null)
        {
            iS3TerritoryHandle territoryHandle = null;

            using (var ctx = new iS3MainDbContext())
            {
                territoryHandle = await getTerritoryHandle(parentNameOrID, ctx);

                if (territoryHandle == null)
                {
                    throw new Exception("Territory null and no default");
                }

                bool exist = territoryHandle.DomainHandles.Any(c => c.Name == name);
                if (exist)
                {
                    throw new Exception("Already exists");
                }

                iS3DomainHandle domainHandle = new iS3DomainHandle();
                domainHandle.ID       = Guid.NewGuid().ToString();
                domainHandle.Name     = name;
                domainHandle.Type     = type;
                domainHandle.ParentID = territoryHandle.ID;

                if (domainHandle.DbName == null)
                {
                    if (territoryHandle.DbName != null)
                    {
                        domainHandle.DbName = territoryHandle.DbName;
                    }
                    else
                    {
                        domainHandle.DbName = MiniServer.DefaultDatabase;
                    }
                }

                territoryHandle.DomainHandles.Add(domainHandle);
                await ctx.SaveChangesAsync();

                return(domainHandle);
            }
        }
Beispiel #2
0
        // Add a new territory handle:
        //   name,  should be filled
        //   if type is not give, i.e., null, default is iS3SimpleTerritory
        //   if dbName is not given, i.e., null, default database name will be used.
        //
        public static async Task <iS3TerritoryHandle> AddTerritoryHandle(string name,
                                                                         string type = null, string dbName = null)
        {
            if (name == null)
            {
                throw new Exception("Argument Null");
            }
            if (type == null)
            {
                type = typeof(iS3SimpleTerritory).ToString();
            }

            using (var ctx = new iS3MainDbContext())
            {
                bool exists = await ctx.TerritoryHandles.AnyAsync(c => c.Name == name);

                if (exists)
                {
                    throw new Exception("Already exists");
                }

                iS3TerritoryHandle newTerritoryHandle = new iS3TerritoryHandle();
                newTerritoryHandle.ID   = Guid.NewGuid().ToString();
                newTerritoryHandle.Name = name;
                newTerritoryHandle.Type = type;
                if (dbName == null)
                {
                    newTerritoryHandle.DbName = MiniServer.DefaultDatabase;
                }
                else
                {
                    newTerritoryHandle.DbName = dbName;
                }

                var result = ctx.TerritoryHandles.Add(newTerritoryHandle);
                await ctx.SaveChangesAsync();

                return(newTerritoryHandle);
            }
        }