using
EntityFramework6Library.Models;
namespace
EntityFramework6Library
{
System.Data.Entity;
public
partial
class
NorthWindContext : DbContext
NorthWindContext() :
base
(
"name=NorthWindContext"
)
}
virtual
DbSet<Contact> Contacts {
get
;
set
; }
DbSet<ContactType> ContactTypes {
protected
override
void
OnModelCreating(DbModelBuilder modelBuilder)
#if Dev
"name=DevConnection"
#elif Staging
"name=StagingConnection"
#else
"name=ProductionConnection"
#endif
{ }
"ConnectionsConfiguration": {
"ActiveEnvironment": "Production",
"Development": "Dev connection string goes here",
"Stage": "Stage connection string goes here",
"Production": "Prod connection string goes here"
DbContextConnections
/// <summary>
/// Simple configuration for setting the connection string
/// </summary>
/// <param name="optionsBuilder"></param>
static
NoLogging(DbContextOptionsBuilder optionsBuilder)
optionsBuilder.UseSqlServer(ConfigurationHelper.ConnectionString());
/// Default logging to output window
StandardLogging(DbContextOptionsBuilder optionsBuilder)
optionsBuilder.UseSqlServer(ConfigurationHelper.ConnectionString())
.EnableSensitiveDataLogging()
.LogTo(message => Debug.WriteLine(message));
/// Writes/appends to a file
/// Make sure that the folder exists, as coded folder name is Logs under the app folder.
/// One way to ensure the folder exists is to use MsBuild task MakeDir as in
/// the test project ShadowPropertiesUnitTestProject.
CustomLogging(DbContextOptionsBuilder optionsBuilder)
.LogTo(
new
DbContextLogger().Log)
.EnableDetailedErrors();
/// <param name="fileName">File name to write log data too</param>
CustomLogging(DbContextOptionsBuilder optionsBuilder,
string
fileName)
DbContextLogger(fileName).Log)
OnConfiguring(DbContextOptionsBuilder optionsBuilder)
if
(!optionsBuilder.IsConfigured)
DbContextConnections.NoLogging(optionsBuilder);
DbContextConnections.StandardLogging(optionsBuilder);
DbContextConnections.CustomLogging(optionsBuilder);
#warning To protect potentially sensitive information in your connection string, you should move it out of source code. See http://go.microsoft.com/fwlink/?LinkId=723263 for guidance on storing connection strings.
optionsBuilder.UseSqlServer(
"Data Source=.\\SQLEXPRESS;Initial Catalog=NorthWind2020;Integrated Security=True"
);
Microsoft.EntityFrameworkCore;
EntityFrameworkCoreLibrary.Models;
System.Configuration.ConfigurationManager;
EntityFrameworkCoreLibrary.Contexts
NorthWindContext()
NorthWindContext(DbContextOptions<NorthWindContext> options)
:
(options)
DbSet<ContactType> ContactType {
DbSet<Contacts> Contacts {
var environment =
""
environment =
"DevConnection"
"StagingConnection"
"ProductionConnection"
optionsBuilder.UseSqlServer(AppSettings[environment]);
OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.ApplyConfiguration(
ContactTypeConfiguration());
ContactsConfiguration());
OnModelCreatingPartial(modelBuilder);
OnModelCreatingPartial(ModelBuilder modelBuilder);