public static void ConfigureContext(this ModelBuilder modelBuilder, AppStoreOptions storeOptions) { if (!string.IsNullOrWhiteSpace(storeOptions.DefaultSchema)) { modelBuilder.HasDefaultSchema(storeOptions.DefaultSchema); } modelBuilder.Entity <Lookup>(lookup => { lookup.ToTable(storeOptions.Lookup); lookup.HasKey(x => x.Id); lookup.Property(x => x.Name).HasMaxLength(200).IsRequired(); lookup.HasIndex(x => x.Name).IsUnique(); }); modelBuilder.Entity <Content>(content => { content.ToTable(storeOptions.Content); content.HasKey(x => x.Id); content.Property(x => x.Data).HasMaxLength(1000).IsRequired(); content.HasOne(x => x.Lookup).WithMany(x => x.Contents).OnDelete(DeleteBehavior.ClientSetNull); }); }
public AppDbContext(DbContextOptions <AppDbContext> options, AppStoreOptions storeOptions) : base(options) { if (storeOptions == null) { throw new ArgumentNullException(nameof(storeOptions)); } StoreOptions = storeOptions; }
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { var connectionString = Configuration.GetConnectionString("SqlServer"); var migrationsAssembly = typeof(Startup).GetTypeInfo().Assembly.GetName().Name; var storeOptions = new AppStoreOptions(); services.AddSingleton(storeOptions); services .AddEntityFrameworkSqlServer() .AddDbContext <Data.Concrete.AppDbContext>((serviceProvider, options) => options.UseSqlServer(connectionString, builder => builder.MigrationsAssembly(migrationsAssembly)) .UseInternalServiceProvider(serviceProvider)); services.AddMvc(); }