Skip to content

Instantly share code, notes, and snippets.

@mehmetalierol
Created September 29, 2018 12:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mehmetalierol/cb48f72c114e6a5a13520c230344553c to your computer and use it in GitHub Desktop.
Save mehmetalierol/cb48f72c114e6a5a13520c230344553c to your computer and use it in GitHub Desktop.
using Company.Application.Data.Entities;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.IO;
namespace Company.Application.Data.Context
{
/// <summary>
/// Bu sınıf bizim DbContext sınıfımız yani database ile ilgili tanımlamaların ve bağlantıların bulunduğu sınıf
/// Normal şartlarda DbContext base sınıfından kalıtım almamız gerekiyor ancak projede Identity alt yapısını kullanacağımız için
/// IdentityDbContext kullanıyoruz, User ve Role sınıflarımızı bu generic base class a tip olarak gönderiyoruz.
/// Aslında IdentityDbContext i inceleyecek olursanız onunda DbContext base classından kalıtım aldığını görebilirsiniz.
/// </summary>
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, Guid, IdentityUserClaim<Guid>, ApplicationUserRole, ApplicationUserLogin, IdentityRoleClaim<Guid>, ApplicationUserToken>
{
#region Constructor
/// <summary>
/// Constructor yani yapıcı method bizim için bu sınıf türetildiğinde devreye ilk girecek olan kısımdır.
/// Constructor DbContextOptions isminde bir sınıf türetilmesini sağlıyor ve bu sınıfı kalıtım aldığı IdentityDbContext sınıfına parametre olarak gönderiyor.
/// </summary>
/// <param name="options"></param>
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
#endregion Constructor
#region ModelCreating
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
//Loglamayı aktif hale getiriyoruz
modelBuilder.EnableAutoHistory(null);
modelBuilder.Entity<ApplicationUserRole>().HasKey(p => new { p.UserId, p.RoleId });
}
#endregion ModelCreating
#region OnConfiguring
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
optionsBuilder.UseSqlServer(config.GetConnectionString("DefaultConnection"));
optionsBuilder.UseLazyLoadingProxies();
}
#endregion OnConfiguring
#region DbSets
/// <summary>
/// Projemiz içerisinde kullanacağımız tüm entity sınıflarımızın bu kısımda DbSet ile tanımlıyoruz
/// Bu sayede migration yaparken bu sınıflar veritabanında birer tablo olarak oluşturulacak. ve yapacağımız CRUD işlemler veritabanına yansıyacak.
/// </summary>
public DbSet<Customer> Customers { get; set; }
public DbSet<Organization> Organizations { get; set; }
public DbSet<Language> Languages { get; set; }
public DbSet<AppResource> AppRecource { get; set; }
#endregion DbSets
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment