// 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); } }
// 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); } }