Пример #1
0
        static void SQL_qry(tect_CodeFirstContext ctx)
        {
            Console.WriteLine("Native SQL query. Find key ");
            var lect1 = ctx.lecturers.SqlQuery("select * from lecturers where lc_id = 'L_1';");

            Console.WriteLine(lect1.ElementAt(0).lc_id);
        }
Пример #2
0
            public void StartOwnTransactionWithinContext()
            {
                using (var ctx = new tect_CodeFirstContext())
                {
                    using (var dbContextTransaction = ctx.Database.BeginTransaction())
                    {
                        try
                        {
                            var lecturerList = ctx.lecturers.ToList <lecturer>();
                            //Perform create operation
                            Console.WriteLine("Perform create operation");
                            ctx.lecturers.Add(new lecturer()
                            {
                                lc_id = "L_2", lc_fname = "Second  lecturer"
                            });
                            //Execute Inser, Update &Delete queries in the database
                            ctx.SaveChanges();
                            var lects1 = ctx.lecturers.ToList <lecturer>();
                            //Perform Update operation
                            Console.WriteLine("Perform Update operation");
                            var lectureList1 = ctx.lecturers.ToList <lecturer>();
                            Console.WriteLine("ojxm");
                            //lecturer lecturerToUpdate = lectureList1.Where(s => s.lc_fname == "Second lecturer").FirstOrDefault<lecturer>();
                            //lecturerToUpdate.lc_fname = "Edited second lecturer";


                            ctx.lecturers.ToList <lecturer>().ElementAt(1).lc_fname = "Edited second lecturer";
                            //Execute Inser, Update & Delete queries in the database

                            ctx.SaveChanges();
                            dbContextTransaction.Commit();
                        }
                        catch (Exception)
                        {
                            dbContextTransaction.Rollback();
                        }
                    }
                }
            }
Пример #3
0
        static void Main(string[] args)
        {
            using (var ctx = new tect_CodeFirstContext())
            {
                try
                {
                    try
                    {
                        ctx.lecturers.Remove(ctx.lecturers.ToList <lecturer>().ElementAt(0));
                        // ctx.lecturers.Remove(ctx.lecturers.ToList<lecturer>().ElementAt(1));
                        ctx.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    var lectureList = ctx.lecturers.ToList <lecturer>();
                    //Perform create operation
                    Console.WriteLine("Perform create operation");
                    ctx.lecturers.Add(new lecturer()
                    {
                        lc_id = "L_1", lc_fname = "New lecturer"
                    });
                    //Execute Insert, Update & Delete queries in the database
                    ctx.SaveChanges();
                    var lects1 = ctx.lecturers.ToList <lecturer>();
                    Lecturers_print(lects1);

                    //Perform Update operation
                    Console.WriteLine("Perform Update operation");
                    var      lecturerList1    = ctx.lecturers.ToList <lecturer>();
                    lecturer lecturerToUpdate = lecturerList1.Where(s => s.lc_fname == "New lecturer").FirstOrDefault <lecturer>();
                    lecturerToUpdate.lc_fname = "Edited lecturer";
                    //Execute Insert, Update & Delete queries in the database
                    ctx.SaveChanges();
                    lects1 = ctx.lecturers.ToList <lecturer>();
                    Lecturers_print(lects1);

                    Console.WriteLine();
                    SQL_qry(ctx);
                    Console.WriteLine();

                    Console.WriteLine("Transaction example");
                    Transaction_test tr = new Transaction_test();
                    tr.StartOwnTransactionWithinContext();

                    lects1 = ctx.lecturers.ToList <lecturer>();
                    Lecturers_print(lects1);
                    //Perform delete operation, re-read lecturers list
                    Console.WriteLine("Perform delete operation");
                    lecturerList1 = ctx.lecturers.ToList <lecturer>();
                    ctx.lecturers.Remove(lecturerList1.ElementAt <lecturer>(0));
                    ctx.lecturers.Remove(lecturerList1.Where(s => s.lc_fname == "Edited second lecturer").FirstOrDefault <lecturer>());
                    //Execute Insert, Update & Delete queries in the database
                    ctx.SaveChanges();
                    lects1 = ctx.lecturers.ToList <lecturer>();
                    Lecturers_print(lects1);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                Console.ReadKey();
            }
        }