-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
CosmosDbContextOptionsExtensions.cs
78 lines (70 loc) · 3.85 KB
/
CosmosDbContextOptionsExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Cosmos.Infrastructure.Internal;
using Microsoft.EntityFrameworkCore.Utilities;
// ReSharper disable once CheckNamespace
namespace Microsoft.EntityFrameworkCore
{
/// <summary>
/// Cosmos-specific extension methods for <see cref="DbContextOptionsBuilder" />.
/// </summary>
public static class CosmosDbContextOptionsExtensions
{
/// <summary>
/// Configures the context to connect to an Azure Cosmos database.
/// </summary>
/// <typeparam name="TContext"> The type of context to be configured. </typeparam>
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
/// <param name="accountEndpoint"> The account end-point to connect to. </param>
/// <param name="accountKey"> The account key. </param>
/// <param name="databaseName"> The database name. </param>
/// <param name="cosmosOptionsAction"> An optional action to allow additional Cosmos-specific configuration. </param>
/// <returns> The options builder so that further configuration can be chained. </returns>
public static DbContextOptionsBuilder<TContext> UseCosmos<TContext>(
[NotNull] this DbContextOptionsBuilder<TContext> optionsBuilder,
[NotNull] string accountEndpoint,
[NotNull] string accountKey,
[NotNull] string databaseName,
[CanBeNull] Action<CosmosDbContextOptionsBuilder> cosmosOptionsAction = null)
where TContext : DbContext
=> (DbContextOptionsBuilder<TContext>)UseCosmos(
(DbContextOptionsBuilder)optionsBuilder,
accountEndpoint,
accountKey,
databaseName,
cosmosOptionsAction);
/// <summary>
/// Configures the context to connect to a Azure Cosmos database.
/// </summary>
/// <param name="optionsBuilder"> The builder being used to configure the context. </param>
/// <param name="accountEndpoint"> The account end-point to connect to. </param>
/// <param name="accountKey"> The account key. </param>
/// <param name="databaseName"> The database name. </param>
/// <param name="cosmosOptionsAction"> An optional action to allow additional Cosmos-specific configuration. </param>
/// <returns> The options builder so that further configuration can be chained. </returns>
public static DbContextOptionsBuilder UseCosmos(
[NotNull] this DbContextOptionsBuilder optionsBuilder,
[NotNull] string accountEndpoint,
[NotNull] string accountKey,
[NotNull] string databaseName,
[CanBeNull] Action<CosmosDbContextOptionsBuilder> cosmosOptionsAction = null)
{
Check.NotNull(optionsBuilder, nameof(optionsBuilder));
Check.NotNull(accountEndpoint, nameof(accountEndpoint));
Check.NotEmpty(accountKey, nameof(accountKey));
Check.NotEmpty(databaseName, nameof(databaseName));
var extension = optionsBuilder.Options.FindExtension<CosmosOptionsExtension>()
?? new CosmosOptionsExtension();
extension = extension
.WithAccountEndpoint(accountEndpoint)
.WithAccountKey(accountKey)
.WithDatabaseName(databaseName);
((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(extension);
cosmosOptionsAction?.Invoke(new CosmosDbContextOptionsBuilder(optionsBuilder));
return optionsBuilder;
}
}
}