Exemplo n.º 1
0
        public async Task CRUDUsingLINQ()
        {
            // The ConfigFactory static constructor reads the "MyProjectSettings" from appsettings.json
            // or secrets.json and exposes the IUserConfiguration interface. We use that interface
            // to retrieve the connection string mapped to the DatabaseName.
            string ConnectionString = _requirements.UserConfiguration.ConnectionString("DSuite");

            DbContextOptionsBuilder <DSuiteContext> optionsBuilder = new DbContextOptionsBuilder <DSuiteContext>();

            optionsBuilder.UseSqlServer(ConnectionString);
            DSuiteContext dSuiteContext = new DSuiteContext(optionsBuilder.Options);

            // NOTE: All the code above could be replaced with a single line of code.
            //       We write out the code here just to show you what's going on
            //       under the covers:
            //
            //          DSuiteContext dSuiteContext = DBContextFactory.GetDbContext<DSuiteContext>();


            // Equivalent CRUD using just linq
            Car newCar = GenerateCar();

            dSuiteContext.Cars.Add(newCar);
            await dSuiteContext.SaveChangesAsync();

            Car found = (from c in dSuiteContext.Cars where c.CarId == newCar.CarId select c).SingleOrDefault();

            dSuiteContext.Remove(found);
            await dSuiteContext.SaveChangesAsync();
        }
Exemplo n.º 2
0
        public static async Task PopulateDummyCars(int NumberOfCars)
        {
            // Ask the DBContextFactory to create the desired DbContext object for use for database access
            DSuiteContext db = DBContextFactory.GetDbContext <DSuiteContext>();

            for (int i = 0; i < NumberOfCars; ++i)
            {
                Car NewCar = GenerateCar();
                await EFCrud.Create(db, NewCar);
            }
        }
Exemplo n.º 3
0
        public async Task CRUDUsingHelperClass()
        {
            Car newCar = GenerateCar();

            // Demonstrate basic CRUD
            DSuiteContext db = DBContextFactory.GetDbContext <DSuiteContext>();
            await EFCrud.Create(db, newCar);

            Car found = await EFCrud.GetByKey(db, newCar);

            EFCrud.Delete(db, found);
        }
Exemplo n.º 4
0
        public void FindMultipleUsingHelperClass(DSuiteContext dSuite)
        {
            // Demonstrate the deferred execution POWER of IQueryable!
            // Find a small list of cars that we don't need to page through.
            IQueryable <Car> fordBunnies = dSuite.FindMultiple <Car, DSuiteContext>((x) => x.Make == "Ford" && x.Model == "Bunny" && x.Year < 2006, x => x.Year);

            // But I DO want to sort them too. Still no DB call here!
            fordBunnies = fordBunnies.OrderBy(bunny => bunny.Mileage);

            // NOW, go ahead and execute the query against the database
            fordBunnies.DumpData($"\r\n\r\n==============FindMultipleUsingHelperClass==============================");
        }
Exemplo n.º 5
0
        public static Car Delete(Car ToDelete, DSuiteContext db)
        {
            Car Deleted = null;

            if (ToDelete != null)
            {
                db.Cars.Remove(ToDelete);
                db.SaveChanges();
                Deleted = ToDelete;
            }

            return(Deleted);
        }
Exemplo n.º 6
0
        public void FindMultipleUsingLINQ()
        {
            DSuiteContext dSuiteContext = DBContextFactory.GetDbContext <DSuiteContext>();

            // Demonstrate the deferred execution POWER of IQueryable!
            // Find a small list of cars that we don't need to page through.
            IQueryable <Car> linqFordBunnies = (from c in dSuiteContext.Cars where c.Make == "Ford" && c.Model == "Bunny" && c.Year < 2006 select c);

            // But I DO want to sort them too. Still no DB call here!
            linqFordBunnies = linqFordBunnies.OrderBy(bunny => bunny.Mileage);

            // NOW, go ahead and execute the query against the database
            linqFordBunnies.DumpData("\r\n\r\n==============FindMultipleUsingLINQ==============================");
        }
Exemplo n.º 7
0
        public static Car Add(Car NewCar, DSuiteContext db)
        {
            try
            {
                db.Cars.Add(NewCar);
                db.SaveChanges();
            }
            catch (Exception Err)
            {
                _Logger.LogError(Err, "Could not add car");
            }

            return(NewCar);
        }
Exemplo n.º 8
0
        public static Car Delete(int id, DSuiteContext db)
        {
            Car Deleted = null;

            try
            {
                Car ToDelete = Get(id, db);
                Deleted = Delete(ToDelete, db);
            }
            catch (Exception Err)
            {
                _Logger.LogError(Err, "Could not delete car");
            }

            return(Deleted);
        }
Exemplo n.º 9
0
        public static Car Update(Car Update, DSuiteContext db)
        {
            try
            {
                Car myCar = (from c in db.Cars where c.CarId == Update.CarId select c).FirstOrDefault();
                if (myCar != null)
                {
                    db.Entry(myCar).CurrentValues.SetValues(Update);
                    db.SaveChanges();
                }
            }
            catch (Exception Err)
            {
                _Logger.LogError(Err, "Could not update car");
            }

            return(Update);
        }
Exemplo n.º 10
0
        /// <summary>
        /// Application initialization.
        /// </summary>
        /// <param name="requirements">IApplicationRequirements Supplied by DI. All required interfaces</param>
        /// <param name="dSuiteContext">DbContext for DSuite database</param>
        /// <param name="schoolContext">DbContext for School database</param>
        /// <param name="cars">ICarRepository Example of using repository pattern</param>
        /// <param name="courses">ICourseRepository Example of using repository pattern</param>
        /// <param name="students">IStudentsRepository Example of using repository pattern</param>
        /// <param name="enrollments">IEnrollmentsRepository Example of using repository pattern</param>
        public MyApplication(IApplicationRequirements <MyApplication> requirements,
                             DSuiteContext dSuiteContext,
                             SchoolContext schoolContext,
                             ICarRepository cars,
                             ICourseRepository courses,
                             IStudentRepository students,
                             IEnrollmentRepository enrollments,
                             IDataRepository <Student, int, SchoolContext> genericStudentDataRepository
                             )
        {
            _requirements = requirements;
            _dSuite       = dSuiteContext;
            _school       = schoolContext;
            _cars         = cars;
            _courses      = courses;
            _students     = students;
            _enrollments  = enrollments;

            // Same as the _students interface, except it's an open generic type
            _genericStudentDataRepository = genericStudentDataRepository;

            EFCrud.InitializeLogger(_requirements.ApplicationLogger);
            EFHelper.InitializeLogger(_requirements.ApplicationLogger);
        }
Exemplo n.º 11
0
        public static Car Get(int id, DSuiteContext db)
        {
            Car myCar = (from c in db.Cars where c.CarId == id select c).FirstOrDefault();

            return(myCar);
        }
Exemplo n.º 12
0
 public static Car Get(Car toGet, DSuiteContext db)
 {
     return(Get(toGet.CarId, db));
 }