public static void Seed(this BookstoreDbContext context) { if (!context.Role.Any()) { context.Add(new Role { Name = "Admin" }); context.Add(new Role { Name = "User" }); context.SaveChanges(); } if (!context.User.Any()) { var adminUser = new User { Email = "*****@*****.**", FirstName = "Admin", LastName = "Admin", Username = "******", Password = "******" }; context.Add(adminUser); context.SaveChanges(); if (!context.UserRoleMapping.Any()) { context.UserRoleMapping.Add(new UserRoleMapping { UserId = adminUser.Id, RoleId = context.Role.FirstOrDefault(x => x.Name == "Admin")?.Id ?? throw new InvalidOperationException() });
private static BookstoreDbContext CreateDatabase() { Database.SetInitializer(new DropCreateDatabaseAlways <BookstoreDbContext>()); var bookStoreDb = new BookstoreDbContext(); bookStoreDb.Authors.Any(); return(bookStoreDb); }
public static void Main() { System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; Console.WriteLine("USE . !!!"); Console.WriteLine(); db = new BookstoreDbContext(); Import(); //Search(); }
private static void Main() { BookstoreDbContext dbContext = CreateDatabase(); BooksXmlParser booksParser = new BooksXmlParser(dbContext, new ConsoleLogger()); booksParser.Parse("../../../complex-book.xml"); QueriesXmlParser queriesParser = new QueriesXmlParser(dbContext, new ConsoleLogger(), "../../../reviews-search-results.xml"); queriesParser.Parse("../../../reviews-queries.xml"); }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env, BookstoreDbContext dbContext) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } else { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); } dbContext.Database.EnsureCreated(); //app.UseCors("AllowAll"); app.UseHttpsRedirection(); app.UseStaticFiles(); if (!env.IsDevelopment()) { app.UseSpaStaticFiles(); } app.UseRouting(); app.UseCors(builder => { builder.AllowAnyOrigin(); builder.AllowAnyMethod(); builder.AllowAnyHeader(); }); app.UseMiddleware <JwtMiddleware>(); app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller}/{action=Index}/{id?}"); }); app.UseSpa(spa => { // To learn more about options for serving an Angular SPA from ASP.NET Core, // see https://go.microsoft.com/fwlink/?linkid=864501 spa.Options.SourcePath = "ClientApp"; if (env.IsDevelopment()) { spa.UseAngularCliServer(npmScript: "start"); } }); }
static void Main() { Database.SetInitializer(new MigrateDatabaseToLatestVersion<BookstoreDbContext, Configuration>()); var db = new BookstoreDbContext(); db.Authors.Count(); var xmlController = new XmlController<CatalogXml>(); var books = xmlController.Deserialize(XmlInputPath).Books; foreach (var book in books) { var authors = new List<Author>(); if (book.Authors != null) { foreach (var auth in book.Authors) { authors.Add(new Author() { Name = auth }); } } var reviews = new List<Review>(); if (book.Reviews != null) { foreach (var review in book.Reviews) { reviews.Add(new Review() { Author = new Author() { Name = review.Author == null ? null : review.Author }, CreationDate = review.Date != null ? DateTime.Parse(review.Date) : DateTime.MinValue }); } } var bookToAdd = db.Books.Add(new Book() { Title = book.Title, Isbn = book.Isbn, Price = book.Price, WebSite = book.Website }); bookToAdd.Reviews = reviews; bookToAdd.Authors = authors; db.Books.Add(bookToAdd); } db.SaveChanges(); }
static void Main(string[] args) { var optionsBuilder = new DbContextOptionsBuilder <BookstoreDbContext>(); optionsBuilder.UseSqlServer(ConnectionString); var context = new BookstoreDbContext(optionsBuilder.Options); context.Database.Migrate(); var saeedGanji = Author.Create("Saeed", "Ganji", new DateTime(1985, 1, 1)); context.Authors.Add(saeedGanji); context.SaveChanges(); // // context.Database.Migrate(); // context.Database.EnsureCreated(); // // var me = Author.Create("Saeed", "Ganji", DateTime.Now.AddDays(-1)); // me.AddBook("EF Core" , "132465"); // // var other = Author.Create("John", "Doe", DateTime.Now.AddDays(-1)); // other.AddBook("No Idea", "132465"); // // context.Authors.AddRange(me,other); // // context.SaveChanges(); Author me = null; // mention AsNoTracking in the article me = context.Authors.AsNoTracking().FirstOrDefault(x => x.FirstName == "Saeed"); me.AddBook("SampleBook", "1234567-890"); context.SaveChanges(); Console.WriteLine(context.Entry(me).Property("CreatedOn").CurrentValue); }
private static void AddTestData(BookstoreDbContext context) { context.Database.ExecuteSqlCommand("Delete From Books"); var book1 = new Book(); book1.Title = "Anna Karenina"; book1.Author = "Leo Tolstoy"; context.Books.Add(book1); var book2 = new Book(); book2.Title = "Into the Wildnerness"; book2.Author = "Sarah Donati"; context.Books.Add(book2); var book3 = new Book(); book3.Title = "Harry Potter, Chamber of Secrets"; book3.Author = "JK Rowling"; context.Books.Add(book3); context.SaveChanges(); }
public GenericRepository(BookstoreDbContext _context) { this._context = _context; table = _context.Set <T>(); }
public Query(BookstoreDbContext dbContext, IMapper mapper) { _dbContext = dbContext; _mapper = mapper; }
public UserRepository(BookstoreDbContext context) : base(context) { }
private readonly ILogger <DbInitializer> logger; // Логирование в консоли public DbInitializer(BookstoreDbContext db, ILogger <DbInitializer> Logger) { this.db = db; logger = Logger; }
public BookDbRepository(BookstoreDbContext _db) { db = _db; }
public BooksController(BookstoreDbContext _context) { context = _context; }
public AuthorDbRepository(BookstoreDbContext _db) { db = _db; }
public XmlParser(BookstoreDbContext context, ILogger logger) { this.BookstoreDb = context; this.Logger = logger; }
public OrderService() { db = new BookstoreDbContext(); }
public ChuDeDAO() { DbContext = new BookstoreDbContext(); }
public QueriesXmlParser(BookstoreDbContext context, ILogger logger, string outputFile) : base(context, logger) { this.rootElement = new XElement("search-results"); this.outputFilePath = outputFile; }
public AuthorDbRepository(BookstoreDbContext db) { this._db = db; }
public AuthorRepository(BookstoreDbContext dbContext) { _dbContext = dbContext; }
public DealsRepository(BookstoreDbContext db) : base(db) { }
public RentalRepository(BookstoreDbContext context) : base(context) { }
public GroupService() { db = new BookstoreDbContext(); }
public AccountService() { db = new BookstoreDbContext(); }
public TransactionRepository(BookstoreDbContext context) : base(context) { }
public AuthorsController(BookstoreDbContext context) { _context = context; }
public WriterRepository(BookstoreDbContext bookstoreDb) { BookstoreDb = bookstoreDb; }