73 lines
2.5 KiB
C#
73 lines
2.5 KiB
C#
using DG.EntityFramework;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.EntityFrameworkCore.Diagnostics;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Zxd.EntityFramework
|
|
{
|
|
public class CrmDbContext : DbContext
|
|
{
|
|
public CrmDbContext(DbContextOptions<CrmDbContext> options) : base(options)
|
|
{
|
|
}
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
{
|
|
if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development")
|
|
{
|
|
var loggerFactory = new LoggerFactory();
|
|
loggerFactory.AddProvider(new EFLoggerProvider());
|
|
optionsBuilder.UseLoggerFactory(loggerFactory);
|
|
}
|
|
optionsBuilder.ConfigureWarnings(b => b.Ignore(CoreEventId.ContextInitialized));
|
|
base.OnConfiguring(optionsBuilder);
|
|
}
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
{
|
|
modelBuilder.Entity<Product>()
|
|
.HasOne(x => x.Module)
|
|
.WithMany(x => x.Product)
|
|
.HasForeignKey(x => x.Moduleid);
|
|
|
|
modelBuilder.Entity<Product>()
|
|
.HasOne(x => x.ProductGroup)
|
|
.WithMany(x => x.Product)
|
|
.HasForeignKey(x => x.Groupid);
|
|
|
|
modelBuilder.Entity<ProductPackage>()
|
|
.HasOne(x => x.ProductGroup)
|
|
.WithMany(x => x.ProductPackage)
|
|
.HasForeignKey(x => x.Groupid);
|
|
|
|
modelBuilder.Entity<ProductTeacher>()
|
|
.HasOne(x => x.Product)
|
|
.WithMany(x => x.ProductTeachers)
|
|
.HasPrincipalKey(x => x.Id)
|
|
.HasForeignKey(x => x.ProductId);
|
|
|
|
modelBuilder.Entity<ProductTeacher>()
|
|
.HasOne(x => x.Teacher)
|
|
.WithMany(x => x.ProductTeachers)
|
|
.HasPrincipalKey(x => x.Code)
|
|
.HasForeignKey(x => x.TeacherCode);
|
|
modelBuilder.Entity<UserModule>().HasKey(c => new { c.moduleid, c.orderid });
|
|
|
|
base.OnModelCreating(modelBuilder);
|
|
}
|
|
|
|
public DbSet<Product> Product { get; set; }
|
|
|
|
public DbSet<ProductPackage> ProductPackage { get; set; }
|
|
|
|
public DbSet<ProductGroup> ProductGroup { get; set; }
|
|
|
|
public DbSet<ProductTeacher> ProductTeacher { get; set; }
|
|
|
|
public DbSet<Teacher> Teacher { get; set; }
|
|
|
|
public DbSet<Module> Module { get; set; }
|
|
|
|
public DbSet<UserModule> UserModule { get; set; }
|
|
}
|
|
} |