Пример #1
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var context = new LibraryContext();

            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();
            //context.Database.Migrate();

            Console.WriteLine();

            var author = context.Set <Author>()
                         .Include(x => x.Books)
                         .FirstOrDefault();

            Console.WriteLine($"Author - First: {author.Name}; Books: {string.Join(",", author.Books.Select(b => b.Name))}");

            Console.WriteLine();
            Console.WriteLine($"Author - List");
            var authors = context.Set <Author>().ToList();

            authors.ForEach(a => Console.WriteLine(a.Name));

            Console.WriteLine();
            Console.WriteLine("Press enter to exit");
            var h = Console.ReadLine();

            context.Dispose();
        }
Пример #2
0
        private void SeedCategories(LibraryContext context)
        {
            var cat1 = new Category()
            {
                Id   = 1,
                Name = "Coursebook"
            };

            context.Set <Category>().AddOrUpdate(cat1);

            var cat2 = new Category()
            {
                Id   = 2,
                Name = "Novel"
            };

            context.Set <Category>().AddOrUpdate(cat2);

            var cat3 = new Category()
            {
                Id   = 3,
                Name = "Biography"
            };

            context.Set <Category>().AddOrUpdate(cat3);

            context.SaveChanges();
        }
Пример #3
0
 public void Add(T t)
 {
     if (t != null)
     {
         _libDbContext.Set <T>().Add(t);
     }
 }
        public void Add(T entity)
        {
            context.Set <T>().Add(entity);
            context.SaveChanges();

            //context.Entry(entity).State = System.Data.Entity.EntityState.Added;
        }
Пример #5
0
        public async Task <T> AddAsync(T entity, CancellationToken cancellationToken = default)
        {
            await _dbContext.Set <T>().AddAsync(entity);

            await _dbContext.SaveChangesAsync(cancellationToken);

            return(entity);
        }
Пример #6
0
        public async Task <ActionResult> Post(Author author)
        {
            await _libraryContext.Set <Author>().AddAsync(author);

            await _libraryContext.SaveChangesAsync();

            return(Ok());
        }
Пример #7
0
 public async Task <Book> Handle(GetBookByIdQuery query)
 {
     return(await _libraryContext.Set <Book>()
            .Include(e => e.Author)
            .Include(e => e.Category)
            .Include(e => e.Country)
            .Include(e => e.Language)
            .Include(e => e.Publisher)
            .SingleOrDefaultAsync(e => e.Id == query.Id));
 }
Пример #8
0
 public bool Insert(T obj)
 {
     try
     {
         _context.Set <T>().Add(obj);
         _context.SaveChanges();
         return(true);
     }
     catch (Exception)
     {
         return(false);
     }
 }
Пример #9
0
        private void AddJsonDataToContext <T>(string fileName)
            where T : class
        {
            var enumerable = _context.Set <T>()
                             .AsEnumerable();
            var jsonReader = new JsonDataReader();
            var freshData  = jsonReader.GetJsonData <T>(fileName)
                             .Where(x => !PublicationExists(enumerable, x))
                             .ToArray();

            if (freshData.Any())
            {
                _context.Set <T>().AddRange(freshData);
            }
        }
Пример #10
0
        private void SeedBooks(LibraryContext context)
        {
            var book1 = new Book()
            {
                Id              = 1,
                CategoryId      = 1,
                Isbn            = "9788325698679",
                Title           = "THINKING IN JAVA",
                Author          = "Eckel Bruce",
                Page            = 1256,
                Publisher       = "Helion",
                PublicationYear = 2017,
                Available       = "Yes"
            };

            context.Set <Book>().AddOrUpdate(book1);

            var book2 = new Book()
            {
                Id              = 2,
                CategoryId      = 2,
                Isbn            = "9788127698879",
                Title           = "Crime and Punishment",
                Author          = "Fyodor Dostoyevsky",
                Page            = 320,
                Publisher       = "Bottom Of The Hill Publishing",
                PublicationYear = 2005,
                Available       = "Yes"
            };

            context.Set <Book>().AddOrUpdate(book2);

            var book3 = new Book()
            {
                Id              = 3,
                CategoryId      = 3,
                Isbn            = "9788727658809",
                Title           = "Steve Jobs",
                Author          = "Isaacson Walter",
                Page            = 736,
                Publisher       = "Insignis",
                PublicationYear = 2015,
                Available       = "Yes"
            };

            context.Set <Book>().AddOrUpdate(book3);
            context.SaveChanges();
        }
Пример #11
0
        private void SeedOrders(LibraryContext context)
        {
            var idUser = context.Set <User>().Where(u => u.UserName == "Admin").FirstOrDefault().Id;
            var order  = new Order()
            {
                Id            = 1,
                UserId        = idUser,
                BookId        = 1,
                OrderDate     = DateTime.Now,
                ReceptionDate = DateTime.Now.AddDays(2),
                ReturnDate    = DateTime.Now.AddDays(30)
            };

            context.Set <Order>().AddOrUpdate(order);
            context.SaveChanges();
        }
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var context = new LibraryContext();

            context.Add(new Author()
            {
                Id = Guid.NewGuid(), Name = "Santiago Posteguillo"
            });
            context.Add(new Author()
            {
                Id = Guid.NewGuid(), Name = "Simon Scarrow"
            });
            context.Add(new Author()
            {
                Id = Guid.NewGuid(), Name = "Steven Pressfiel"
            });
            context.Add(new Author()
            {
                Id = Guid.NewGuid(), Name = "Orson Scott"
            });
            context.Add(new Author()
            {
                Id = Guid.NewGuid(), Name = "John Scalzi"
            });
            context.SaveChanges();

            Console.WriteLine();
            var author = context.Set <Author>().FirstOrDefault();

            Console.WriteLine($"Author - First: {author.Name}");

            Console.WriteLine();
            Console.WriteLine($"Author - List");
            var authors = context.Set <Author>().ToList();

            authors.ForEach(a => Console.WriteLine(a.Name));

            Console.WriteLine();
            Console.WriteLine("Press any key to exit");
            var h = Console.ReadLine();

            context.Dispose();
        }
Пример #13
0
        public async Task <TEntity> CreateAsync(TEntity item)
        {
            await _context.Set <TEntity>().AddAsync(item);

            await _context.SaveChangesAsync();

            return(item);
        }
Пример #14
0
        public Librarian Add(Librarian libEntity)
        {
            DbEntityEntry dbEntityEntry = _context.Entry(libEntity);

            if (dbEntityEntry != null && dbEntityEntry.State != EntityState.Detached)
            {
                dbEntityEntry.State = EntityState.Added;
            }
            var dbset = _context.Set <Librarian>();

            dbset.Add(libEntity);
            _context.SaveChanges();
            return(libEntity);
        }
Пример #15
0
        public Cardholder Add(Cardholder libEntity)
        {
            DbEntityEntry dbEntityEntry = _context.Entry(libEntity);

            if (dbEntityEntry != null && dbEntityEntry.State != EntityState.Detached)
            {
                dbEntityEntry.State = EntityState.Added;
            }
            var dbset = _context.Set <Cardholder>();

            dbset.Add(libEntity);
            _context.SaveChanges();
            return(libEntity);
        }
Пример #16
0
        public CheckOutLog Add(CheckOutLog libEntity)
        {
            DbEntityEntry dbEntityEntry = _context.Entry(libEntity);

            if (dbEntityEntry != null && dbEntityEntry.State != EntityState.Detached)
            {
                dbEntityEntry.State = EntityState.Added;
            }
            var newDbSet = _context.Set <CheckOutLog>();

            newDbSet.Add(libEntity);
            _context.SaveChanges();
            return(libEntity);
        }
Пример #17
0
        public TEntidade ObterPorId(int id, params Expression <Func <TEntidade, object> >[] includeProperties)
        {
            if (includeProperties.Any())
            {
                return(Listar(includeProperties).FirstOrDefault(x => x.Id.ToString() == id.ToString()));
            }

            return(_context.Set <TEntidade>().Find(id));
        }
Пример #18
0
        public async Task <Book> Handle(CreateBookCommand command)
        {
            var book = new Book
            {
                CategoryId  = command.CategoryId,
                AuthorId    = command.AuthorId,
                Title       = command.Title,
                CountryId   = command.CountryId,
                LanguageId  = command.LanguageId,
                Description = command.Description,
                PublisherId = command.PublisherId
            };

            _libraryContext.Set <Book>().Add(book);
            await _libraryContext.SaveChangesAsync();

            return(_libraryContext.Set <Book>()
                   .Include(e => e.Author)
                   .Include(e => e.Category)
                   .Include(e => e.Country)
                   .Include(e => e.Language)
                   .Include(e => e.Publisher)
                   .SingleOrDefault(e => e.Id == book.Id));
        }
Пример #19
0
        public void Update(TEntity entity)
        {
            if (_dbContext.Entry(entity).State == EntityState.Detached)
            {
                _dbContext.Attach(entity);
            }

            _dbContext.Set <TEntity>().Update(entity);
            _dbContext.SaveChanges();
        }
        public async Task <int> Delete(Expression <Func <T, bool> > func)
        {
            var itemsToRemove = await context.Set <T>().Where(func).ToListAsync();

            foreach (var item in itemsToRemove)
            {
                context.Set <T>().Remove(item);
            }

            return(await context.SaveChangesAsync());
        }
Пример #21
0
 public GenericRepository(LibraryContext _data)
 {
     this._data = _data;
     _dbSet     = _data.Set <T>();
 }
Пример #22
0
 public Repository(LibraryContext context)
 {
     this.context = context;
     entities     = context.Set <T>();
 }
Пример #23
0
 public virtual IEnumerable <T> GetAll()
 {
     return(_context.Set <T>());
 }
Пример #24
0
 public void Delete(T entity)
 {
     context.Set <T>().Remove(entity);
 }
 public GenericRepository(string connection)
 {
     _databaseLibraryContext = new LibraryContext(connection);
     _dbSet = _databaseLibraryContext.Set <TEntity>();
 }
Пример #26
0
 public T Get(long id)
 {
     return(_context.Set <T>().Find(id));
 }
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            var context = new LibraryContext();

            context.Database.EnsureDeleted();
            context.Database.EnsureCreated();
            //context.Database.Migrate();

            context.Add(new Author()
            {
                Id    = Guid.NewGuid(),
                Name  = "Santiago Posteguillo",
                Books = new List <Book>()
                {
                    new Book()
                    {
                        Id   = Guid.NewGuid(),
                        Name = "Africanus"
                    },
                    new Book()
                    {
                        Id   = Guid.NewGuid(),
                        Name = "Las legiones malditas"
                    }
                }
            });
            context.Add(new Author()
            {
                Id = Guid.NewGuid(), Name = "Simon Scarrow"
            });
            context.Add(new Author()
            {
                Id = Guid.NewGuid(), Name = "Steven Pressfiel"
            });
            context.Add(new Author()
            {
                Id = Guid.NewGuid(), Name = "Orson Scott"
            });
            context.Add(new Author()
            {
                Id = Guid.NewGuid(), Name = "John Scalzi"
            });

            context.SaveChanges();

            Console.WriteLine();

            var author = context.Set <Author>()
                         .Include(x => x.Books)
                         .FirstOrDefault();

            Console.WriteLine($"Author - First: {author.Name}; Books: {string.Join(",", author.Books.Select(b => b.Name))}");

            Console.WriteLine();
            Console.WriteLine($"Author - List");
            var authors = context.Set <Author>().ToList();

            authors.ForEach(a => Console.WriteLine(a.Name));

            var books = context.Set <Book>().ToList();



            Console.WriteLine();
            Console.WriteLine("Press enter to exit");
            var h = Console.ReadLine();

            context.Dispose();
        }
Пример #28
0
 public TEntity Add(TEntity entity)
 {
     Context.Set <TEntity>().Add(entity);
     Context.SaveChanges();
     return(entity);
 }
Пример #29
0
 public CheckoutLogRepository()
 {
     _context = new LibraryContext();
     _dbSet   = _context.Set <CheckOutLog>();
 }
Пример #30
0
 public virtual void Add(params TEntity[] toAdd)
 {
     Context.Set <TEntity>().AddRange(toAdd);
 }