using DataLayer.Configurations; using Dinah.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; namespace DataLayer { public class LibationContext : InterceptableDbContext { // IMPORTANT: USING DbSet<> // ======================== // these run against the db. linq queries against these MUST be translatable to sql. primatives only. no POCOs or this error occurs: // Unable to create a constant value of type 'DataLayer.Contributor'. Only primitive types or enumeration types are supported in this context. // to use full object-linq, load and use local // load full table: // List contributors = ...; // Contributors.Load(); // Contributors.Local.Where(a => contributors.Contains(a)); // load only those in object: // // overwrite collection // Entry(product).Collection(x => x.Narrators).Load(); // product.Narrators = narrators; public DbSet Library { get; private set; } public DbSet Books { get; private set; } public DbSet Contributors { get; private set; } public DbSet Series { get; private set; } public DbSet Categories { get; private set; } public static LibationContext Create() { var factory = new LibationContextFactory(); var context = factory.Create(); return context; } // see DesignTimeDbContextFactoryBase for info about ctors and connection strings/OnConfiguring() internal LibationContext(DbContextOptions options) : base(options) { } // called on each instantiation protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { AddInterceptor(new TagPersistenceInterceptor()); base.OnConfiguring(optionsBuilder); } // typically only called once per execution; NOT once per instantiation protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); modelBuilder.ApplyConfiguration(new BookConfig()); modelBuilder.ApplyConfiguration(new ContributorConfig()); modelBuilder.ApplyConfiguration(new BookContributorConfig()); modelBuilder.ApplyConfiguration(new LibraryBookConfig()); modelBuilder.ApplyConfiguration(new SeriesConfig()); modelBuilder.ApplyConfiguration(new SeriesBookConfig()); modelBuilder.ApplyConfiguration(new CategoryConfig()); // seeds go here. examples in scratch pad modelBuilder .Entity() .HasData(Category.GetEmpty()); modelBuilder .Entity() .HasData(Contributor.GetEmpty()); // views are now supported via "query types" (instead of "entity types"): https://docs.microsoft.com/en-us/ef/core/modeling/query-types } } }