// 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 = int.Parse(Guid.NewGuid().ToString()); domainHandle.Name = name; domainHandle.Type = type; domainHandle.ParentID = territoryHandle.ID.ToString(); 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); } }
// 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 = int.Parse(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); } }
// Get territory handle. // Note: // 1. If not found, it will try to return the territory which is the default, // i.e., iS3TerritoryHandle.Default==true // 2. if ctx is null, default context, i.e., the default database will be used. // public static async Task <iS3TerritoryHandle> getTerritoryHandle(string nameOrID, iS3MainDbContext ctx = null) { iS3TerritoryHandle tHandle = null; if (ctx == null) { using (var ctx_new = new iS3MainDbContext()) { tHandle = await getTerritoryHandleInternal(nameOrID, ctx_new); // explicit load domain handles // var entry = ctx_new.Entry(tHandle).Collection(t => t.DomainHandles); var isLoaded = entry.IsLoaded; await entry.LoadAsync(); isLoaded = entry.IsLoaded; return(tHandle); } } else { tHandle = await getTerritoryHandleInternal(nameOrID, ctx); // explicit load domain handles // var entry = ctx.Entry(tHandle).Collection(t => t.DomainHandles); var isLoaded = entry.IsLoaded; await entry.LoadAsync(); isLoaded = entry.IsLoaded; return(tHandle); } }