Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
de35eed
commit 555eb17
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
Connection Strings | ||
================== | ||
|
||
Most database providers require some form of connection string to connect to the database. Sometimes this connection string contains sensitive information that needs to be protected. You may also need to change the connection string as you move your application between environments, such as development, testing, and production. | ||
|
||
.. contents:: `In this article:` | ||
:depth: 2 | ||
:local: | ||
|
||
Full .NET Applications | ||
---------------------- | ||
|
||
Full .NET applications, such as WinForms, WPF, Console, and ASP.NET 4, have a tried and tested connection string pattern. The connection string should be added to your applications App.config file (Web.config if you are using ASP.NET). If your connection string contains sensitive information, such as username and password, you can protect the contents of the configuration file using `Protected Configuration <https://msdn.microsoft.com/en-us/library/53tyfkaw.aspx>`_. | ||
|
||
.. code-block:: xml | ||
:linenos: | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<configuration> | ||
<connectionStrings> | ||
<add name="BloggingDatabase" | ||
connectionString="Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" /> | ||
</connectionStrings> | ||
</configuration> | ||
.. note:: | ||
The ``providerName`` setting is not required on EF Core connection strings stored in App.config because the database provider is configured via code. | ||
|
||
You can then read the connection string using the ``ConfigurationManager`` API in your context's ``OnConfiguring`` method. You may need to add a reference to the ``System.Configuration`` framework assembly to be able to use this API. | ||
|
||
.. code-block:: c# | ||
:linenos: | ||
public class BloggingContext : DbContext | ||
{ | ||
public DbSet<Blog> Blogs { get; set; } | ||
public DbSet<Post> Posts { get; set; } | ||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["BloggingDatabase"].ConnectionString); | ||
} | ||
} | ||
Universal Windows Platform (UWP) | ||
-------------------------------- | ||
|
||
Connection strings in a UWP application are typically a SQLite connection that just specifies a local filename. They typically do not contain sensitive information, and do not need to be changed as an application is deployed. As such, these connection strings are usually fine to be left in code, as shown below. If you wish to move them out of code then UWP supports the concept of settings, see the `App Settings section of the UWP documentation <https://msdn.microsoft.com/windows/uwp/app-settings/store-and-retrieve-app-data>`_ for details. | ||
|
||
.. code-block:: c# | ||
:linenos: | ||
public class BloggingContext : DbContext | ||
{ | ||
public DbSet<Blog> Blogs { get; set; } | ||
public DbSet<Post> Posts { get; set; } | ||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
{ | ||
optionsBuilder.UseSqlite("Filename=Blogging.db"); | ||
} | ||
} | ||
ASP.NET Core | ||
------------ | ||
|
||
In ASP.NET Core the configuration system is very flexible, and the connection string could be stored in ``appsettings.json``, an environment variable, the user secret store, or another configuration source. See the `Configuration section of the ASP.NET Core documentation <https://docs.asp.net/en/latest/fundamentals/configuration.html>`_ for more details. The following example shows the connection string stored in ``appsettings.json``. | ||
|
||
.. code-block:: json | ||
:linenos: | ||
{ | ||
"ConnectionStrings": { | ||
"BloggingDatabase": "Server=(localdb)\\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;" | ||
}, | ||
} | ||
The context is typically configured in ``Startup.cs`` with the connection string being read from configuration. Note the ``GetConnectionString()`` method simply looks for a configuration value whose key is ``ConnectionStrings:<connection string name>``. | ||
|
||
.. code-block:: c# | ||
:linenos: | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddDbContext<BloggingContext>(options => | ||
options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase"))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters