SqlServerExtensions.UseSqlServer<TAdoDbContext>(this IDataContextOptions<TAdoDbContext> contextOptions, string connectionString) Method
.NET Standard 2.x
Configures the context to connect to a SqlServer database.
Namespace: SnapObjects.Data.SqlServer
Assembly: SnapObjects.Data.SqlServer.dll
Syntax
public static IDataContextOptions<TAdoDbContext> UseSqlServer<TAdoDbContext>(this IDataContextOptions<TAdoDbContext> contextOptions, string connectionString) where TAdoDbContext : SqlServerDataContext
Type Parameters
TAdoDbContext
The type of context to be configured.
Parameters
contextOptions
IDataContextOptions<TAdoDbContext>
The options being used to configure the context.
connectionString
System.String
A string containing connection information about how to connect to the data source.
Returns
SnapObjects.Data.IDataContextOptions<TAdoDbContext>
An IDataContextOptions<TAdoDbContext>
object which contains options of the DataContext
object.
Examples
The following code demonstrates how to use the UseSqlServer
method in the ConfigureServices
method of the Startup
class.
using Appeon.ApiDoc.Models;
using Microsoft.Extensions.DependencyInjection;
using SqlServer.ManagedDataAccess.Client;
using SnapObjects.Data;
using SnapObjects.Data.SqlServer;
using System;
using System.Configuration;
using Microsoft.Extensions.Configuration;
namespace Appeon.ApiDoc.SqlServerExtensionsExamples
{
public class StartUp
{
public IConfiguration Configuration { get; }
public StartUp(IConfiguration configuration)
{
Configuration = configuration;
}
const string CONNECTION_STRING = "USER ID=***;PASSWORD=***;" +
"DATA SOURCE=\"(DESCRIPTION=(ADDRESS=(HOST=***)(PORT=1521)(PROTOCOL=TCP))(CONNECT_DATA=(SERVICE_NAME=***)))\";";
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// UseSqlServer with connection string.
services.AddDataContext<SqlServerContext>(m => m.UseSqlServer(CONNECTION_STRING));
// UseSqlServer with connection string and other options.
services.AddDataContext<SqlServerContext>(m => m.UseSqlServer(CONNECTION_STRING, n => n.TrimSpaces = true));
// UseSqlServer with the IConfiguration object and the key specified in the connection strings.
services.AddDataContext<SqlServerContext>(m => m.UseSqlServer(this.Configuration, "MyConnectionString"));
}
}
}
Applies to
.NET Standard
2.x