本文整理汇总了C#中Microsoft.Azure.Documents.Client.DocumentClient.CreateDatabaseAsync方法的典型用法代码示例。如果您正苦于以下问题:C# DocumentClient.CreateDatabaseAsync方法的具体用法?C# DocumentClient.CreateDatabaseAsync怎么用?C# DocumentClient.CreateDatabaseAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.Azure.Documents.Client.DocumentClient
的用法示例。
在下文中一共展示了DocumentClient.CreateDatabaseAsync方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateDatabase
private static void CreateDatabase(DocumentClient documentClient)
{
//create a document collection if it doesn't exist.
try
{
documentClient.ReadDatabaseAsync(UriFactory.CreateDatabaseUri(DatabaseName)).GetAwaiter().GetResult();
}
catch (DocumentClientException de)
{
// If the database does not exist, create it
if (de.StatusCode == HttpStatusCode.NotFound)
{
documentClient.CreateDatabaseAsync(new Database { Id = DatabaseName }).GetAwaiter().GetResult();
}
else
{
throw;
}
}
}
示例2: GetDocumentDbClient
private async Task<DocumentClient> GetDocumentDbClient()
{
var client = new DocumentClient(new Uri(_endPointUrl), _authorizationKKry);
var database = client.CreateDatabaseQuery().
Where(db => db.Id == DocumentDbId).AsEnumerable().FirstOrDefault();
if (database == null)
{
database = await client.CreateDatabaseAsync(
new Database
{
Id = DocumentDbId
});
}
DocumentCollection = client.CreateDocumentCollectionQuery
("dbs/" + database.Id).Where(c => c.Id == _collectionId).AsEnumerable().FirstOrDefault();
if (DocumentCollection == null)
{
DocumentCollection = await client.CreateDocumentCollectionAsync("dbs/" + DocumentDbId,
new DocumentCollection
{
Id = _collectionId
});
}
return client;
}
示例3: GetOrCreateDatabase
private static Database GetOrCreateDatabase(DocumentClient client, string databaseName)
{
// Try to retrieve the database (Microsoft.Azure.Documents.Database) whose Id is equal to databaseName
var database = client.CreateDatabaseQuery().Where(db => db.Id == databaseName).ToArray().FirstOrDefault()
?? client.CreateDatabaseAsync(new Database { Id = databaseName }).Result;
return database;
}
示例4: Initialise
public DocumentClient Initialise()
{
var configuration = (Configuration)ConfigurationManager.GetSection("DocumentDbProvider");
_documentClient = new DocumentClient(new Uri(configuration.Endpoint), configuration.Key);
Database = _documentClient.CreateDatabaseQuery()
.Where(d => d.Id == _databaseName).AsEnumerable().FirstOrDefault()
?? _documentClient.CreateDatabaseAsync(new Database { Id = _databaseName }).Result;
return _documentClient;
}
示例5: ReadOrCreateDatabase
internal static Database ReadOrCreateDatabase(DocumentClient client, string databaseId)
{
var databases = client.CreateDatabaseQuery()
.Where(db => db.Id == databaseId).ToArray();
if (databases.Any())
{
return databases.First();
}
var database = new Database { Id = databaseId };
return client.CreateDatabaseAsync(database).Result;
}
示例6: GetOrCreateDatabase
public static Database GetOrCreateDatabase(DocumentClient client, string databaseId)
{
IQueryable<Database> dbQuery = from db in client.CreateDatabaseQuery()
where db.Id == databaseId
select db;
IEnumerable<Database> enumerable = dbQuery.AsEnumerable();
if (!enumerable.Any())
{
return client.CreateDatabaseAsync(new Database() { Id = databaseId }).Result.Resource;
}
else
{
return enumerable.First<Database>();
}
}
示例7: WriteDocument
public async Task<bool> WriteDocument(MessageType type, String jsonString)
{
var client = new DocumentClient(new Uri(ConfigurationManager.AppSettings["DocumentServiceEndpoint"]), ConfigurationManager.AppSettings["DocumentKey"]);
var dbName = ConfigurationManager.AppSettings["DocumentDatabase"];
var database = client.CreateDatabaseQuery().Where(db => db.Id == dbName).AsEnumerable().FirstOrDefault() ??
await client.CreateDatabaseAsync(new Database{ Id = dbName});
// Check to verify a document collection with the does not exist
var docCollection = ConfigurationManager.AppSettings["DocumentCollection"];
var documentCollection = client.CreateDocumentCollectionQuery(database.CollectionsLink).Where(c => c.Id == docCollection).AsEnumerable().FirstOrDefault() ??
await client.CreateDocumentCollectionAsync(database.CollectionsLink, new DocumentCollection { Id = docCollection });
var id = Guid.NewGuid().ToString();
var response = HttpStatusCode.Unused;
switch (type)
{
case MessageType.Energy:
var energyDoc = JsonConvert.DeserializeObject<EnergyDocument>(jsonString);
energyDoc.id = id;
response = (await client.CreateDocumentAsync(documentCollection.DocumentsLink, energyDoc)).StatusCode;
break;
case MessageType.Humidity:
var humidityDoc = JsonConvert.DeserializeObject<HumidityDocument>(jsonString);
humidityDoc.id = id;
response = (await client.CreateDocumentAsync(documentCollection.DocumentsLink, humidityDoc)).StatusCode;
break;
case MessageType.Light:
var lightDoc = JsonConvert.DeserializeObject<LightDocument>(jsonString);
lightDoc.id = id;
response = (await client.CreateDocumentAsync(documentCollection.DocumentsLink, lightDoc)).StatusCode;
break;
case MessageType.Temperature:
var tempDoc = JsonConvert.DeserializeObject<TemperatureDocument>(jsonString);
tempDoc.id = id;
response = (await client.CreateDocumentAsync(documentCollection.DocumentsLink, tempDoc)).StatusCode;
break;
}
return response == HttpStatusCode.Created;
}
示例8: SaveTestResults
public static async Task SaveTestResults(PerformanceTestResult testResults)
{
// Make sure to call client.Dispose() once you've finished all DocumentDB interactions
// Create a new instance of the DocumentClient
var client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey);
// Check to verify a database with the id=FamilyRegistry does not exist
Database database = client.CreateDatabaseQuery().Where(db => db.Id == "DistributedWebTest").AsEnumerable().FirstOrDefault();
if (database == null)
{
// Create a database
database = await client.CreateDatabaseAsync(
new Database
{
Id = "DistributedWebTest"
});
}
// Check to verify a document collection with the id=FamilyCollection does not exist
DocumentCollection documentCollection = client.CreateDocumentCollectionQuery(database.CollectionsLink).Where(c => c.Id == "DistributedTestResults").AsEnumerable().FirstOrDefault();
if (documentCollection == null)
{
// Create a document collection using the lowest performance tier available (currently, S1)
documentCollection = await client.CreateDocumentCollectionAsync(database.CollectionsLink,
new DocumentCollection { Id = "DistributedTestResults" },
new RequestOptions { OfferType = "S1" });
}
await client.CreateDocumentAsync(documentCollection.DocumentsLink, testResults);
}
示例9: Initialize
void Initialize(EventStorageConfiguration configuration)
{
_client = new DocumentClient(new Uri(configuration.Url), configuration.AuthorizationKey);
_client.ReadDatabaseFeedAsync()
.ContinueWith(a => _database = a.Result.Where(d => d.Id == configuration.DatabaseId).SingleOrDefault())
.Wait();
if (_database == null)
{
_database = new Database { Id = configuration.DatabaseId };
_client.CreateDatabaseAsync(_database)
.ContinueWith(d => _database = d.Result.Resource)
.Wait();
}
}
示例10: CreateDatabase
public static async Task<HttpStatusCode> CreateDatabase(DocumentClient client)
{
var database = await client.CreateDatabaseAsync(new Database() { Id = DbName});
var test = database.StatusCode;
return test;
}
示例11: ReadOrCreateDatabase
/// <summary>
/// Reads or create a database.
/// </summary>
/// <param name="client">The client.</param>
/// <param name="databaseId">The database identifier.</param>
public static async Task<Microsoft.Azure.Documents.Database> ReadOrCreateDatabase(DocumentClient client, string databaseId)
{
// Get the database
var db = client.CreateDatabaseQuery().Where(d => d.Id == databaseId).AsEnumerable().FirstOrDefault();
// Return the database or create it if not exists yet
return db ?? await client.CreateDatabaseAsync(new Microsoft.Azure.Documents.Database { Id = databaseId });
}
示例12: CreateDocumentDbConnection
private DocumentConnection CreateDocumentDbConnection()
{
var client = new DocumentClient(new Uri(EndpointUri), AccountKey);
// get the database and if it doesn't exist create it
Database database = client.CreateDatabaseQuery()
.Where(db => db.Id == Database)
.AsEnumerable()
.FirstOrDefault();
if (database == null)
{
Log.LogMessage(MessageImportance.Low, "The database {0} does not exist, will create it.", Database);
var task = client.CreateDatabaseAsync(new Database { Id = Database });
database = task.Result;
}
// get the document collection and if it doesn't exist create it
DocumentCollection collection = client.CreateDocumentCollectionQuery(database.SelfLink)
.Where(c => c.Id == Collection)
.AsEnumerable()
.FirstOrDefault();
if (collection == null)
{
Log.LogMessage(MessageImportance.Low, "The collection {0} does not exist, will create it.", Collection);
var task = client.CreateDocumentCollectionAsync(database.SelfLink, new DocumentCollection { Id = Collection });
collection = task.Result;
}
Log.LogMessage(MessageImportance.Normal, "Connected to DocumentDB database {0}, collection {1}.", Database, Collection);
return new DocumentConnection(database, client, collection);
}
示例13: InitializeDocumentDb
/// <summary>
/// Initialize the DocumentDb settings and connections
/// </summary>
public void InitializeDocumentDb()
{
this.DocumentDbEndPointUrl = ConfigurationManager.AppSettings["DocumentDbEndPointUrl"];
if (String.IsNullOrWhiteSpace(this.DocumentDbEndPointUrl))
{
throw new ArgumentException("A required AppSetting cannot be null or empty", "DocumentDbEndPointUrl");
}
this.DocumentDbAuthorizationKey = ConfigurationManager.AppSettings["DocumentDbAuthorizationKey"];
if (String.IsNullOrWhiteSpace(this.DocumentDbAuthorizationKey))
{
throw new ArgumentException("A required AppSetting cannot be null or empty", "DocumentDbAuthorizationKey");
}
this.DocumentDbDatabase = ConfigurationManager.AppSettings["DocumentDbDatabase"];
if (String.IsNullOrWhiteSpace(this.DocumentDbDatabase))
{
throw new ArgumentException("A required AppSetting cannot be null or empty", "DocumentDbDatabase");
}
this.DocumentDbCollection = ConfigurationManager.AppSettings["DocumentDbCollection"];
if (String.IsNullOrWhiteSpace(this.DocumentDbCollection))
{
throw new ArgumentException("A required AppSetting cannot be null or empty", "DocumentDbCollection");
}
// Create a new instance of the DocumentClient
documentClient = new DocumentClient(new Uri(this.DocumentDbEndPointUrl), this.DocumentDbAuthorizationKey);
// Check to verify if database already exists
Database database = documentClient.CreateDatabaseQuery().
Where(db => db.Id == this.DocumentDbDatabase).AsEnumerable().FirstOrDefault();
if (database == null)
{
Context.Logger.Info("Creating a new DocumentDb database: {0}", this.DocumentDbDatabase);
// Create a database
var task = documentClient.CreateDatabaseAsync(
new Database
{
Id = this.DocumentDbDatabase
});
task.Wait();
database = task.Result;
}
else
{
Context.Logger.Info("Found an existing DocumentDb database: {0}", database.Id);
}
// Check to verify a document collection already exists
documentCollection = documentClient.CreateDocumentCollectionQuery(database.CollectionsLink).
Where(c => c.Id == this.DocumentDbCollection).AsEnumerable().FirstOrDefault();
if (documentCollection == null)
{
Context.Logger.Info("Creating a new DocumentDb collection: {0}", this.DocumentDbCollection);
// Create a document collection
var task = documentClient.CreateDocumentCollectionAsync(database.CollectionsLink,
new DocumentCollection
{
Id = this.DocumentDbCollection,
});
task.Wait();
documentCollection = task.Result;
}
else
{
Context.Logger.Info("Found an existing DocumentDb collection: {0}", documentCollection.Id);
}
}
示例14: CreateDatabase
private static async Task<Database> CreateDatabase(DocumentClient documentClient)
{
Database database = documentClient.CreateDatabaseQuery().Where(c => c.Id == "courseDatabase").AsEnumerable().FirstOrDefault();
if (database == null)
{
database = await documentClient.CreateDatabaseAsync(new Database()
{
Id = "courseDatabase"
});
}
return database;
}
示例15: GetDatabase
private static async Task<Database> GetDatabase(DocumentClient client)
{
Database database = client.CreateDatabaseQuery().Where(c => c.Id == "SchoolRegistry").AsEnumerable().FirstOrDefault();
if (database == null)
{
database = await client.CreateDatabaseAsync(new Database() { Id = "SchoolRegistry" });
}
return database;
}