Example #1
0
        static void Main(string[] args)
        {
            using (var db = new SQLiteDBContext())
            {
                // Create
                Console.WriteLine("Add New Employee: ");


                var NuevoTeam = new Team {
                    NameTeam = "Sistemas"
                };
                db.Teams.Add(NuevoTeam);
                var Developer = new TeamMembership {
                    Team = NuevoTeam, Role = "Developer"
                };
                db.TeamMemberships.Add(Developer);
                var Employee1 = new Employee {
                    FirstName = "John", LastName = "Doe", Age = 55, TeamMembership = new List <TeamMembership>()
                };
                //db.Employees.Add(new Employee { FirstName = "John", LastName = "Doe", Age = 55, TeamMembership = Developer });
                Employee1.TeamMembership.Add(Developer);
                db.Employees.Add(Employee1);
                db.SaveChanges();
                Console.WriteLine("Employee has been added sucessfully.");


                // Read
                Console.WriteLine("Querying table for that employee.");
                var employee = db.Employees
                               .OrderBy(b => b.Id)
                               .First();

                Console.WriteLine("The employee found: {0} {1} and is {2} years old and he is {3}.", employee.FirstName, employee.LastName, employee.Age, employee.TeamMembership);



                // Update
                Console.WriteLine("Updating the employee first name and age.");

                employee.FirstName = "Louis";
                employee.Age       = 90;


                Console.WriteLine("Newly updated employee is: {0} {1} and is {2} years old  and he is : {3}.", employee.FirstName, employee.LastName, employee.Age, employee.TeamMembership);

                db.SaveChanges();

                // Delete
                Console.WriteLine("Delete the employee.");

                db.Remove(employee);
                db.SaveChanges();
            }
        }
Example #2
0
 public static void CreateDB()
 {
     using (var context = new SQLiteDBContext())
     {
         context.Database.EnsureCreated();
         context.SaveChanges();
     }
 }
Example #3
0
        static void Main(string[] args)
        {
            using (var db = new SQLiteDBContext())
            {
                // Create
                Console.WriteLine("Add New Employee: ");
                db.Employees.Add(new Employee {
                    FirstName = "John", LastName = "Doe", Age = 55
                });
                db.SaveChanges();

                Console.WriteLine("Employee has been added sucessfully.");

                // Read
                Console.WriteLine("Querying table for that employee.");
                var employee = db.Employees
                               .OrderBy(b => b.Id)
                               .First();

                Console.WriteLine("The employee found: {0} {1} and is {2} years old.", employee.FirstName, employee.LastName, employee.Age);

                // Update
                Console.WriteLine("Updating the employee first name and age.");

                employee.FirstName = "Louis";
                employee.Age       = 90;

                Console.WriteLine("Newly updated employee is: {0} {1} and is {2} years old.", employee.FirstName, employee.LastName, employee.Age);

                db.SaveChanges();

                // Delete
                Console.WriteLine("Delete the employee.");

                db.Remove(employee);
                db.SaveChanges();
                Console.ReadKey();
            }
        }
Example #4
0
 public static void AddToDb(Dictionary <string, string> dictionary)
 {
     if (dictionary.Count > 0)
     {
         using (var context = new SQLiteDBContext())
         {
             foreach (var item in dictionary)
             {
                 if (context.Realtys.Where(x => x.Id == item.Key).ToList().Count < 1)
                 {
                     context.Realtys.Add(new Realty {
                         Id = item.Key, Url = item.Value
                     });
                 }
             }
             context.SaveChanges();
         }
     }
 }
Example #5
0
        //public static void ParseAd()
        //{
        //    //CreateDB();
        //    Parser.Log += "Starting parsing/updating all ads.".ToLogFormat();
        //    List<Realty> list = new List<Realty> { };
        //    int count = 0;
        //    using (var context = new SQLiteDBContext())
        //    {
        //        list = context.Realtys.ToList();
        //    }
        //    foreach (var o in list)
        //    {
        //        count++;
        //        try
        //        {
        //            using (var context = new SQLiteDBContext())
        //            {
        //                //Parser.Log += $"Parsing {o.Url} page".ToLogFormat();
        //                string page = null;
        //                while (page == null)
        //                    page = Parser.GetPage(o.Url, Parser.ProxyTimeout);
        //                var item = context.Realtys.FirstOrDefault(x => x.Id == o.Id);
        //                if (page == "skip")
        //                {
        //                    Parser.Log += $"Page {count}/{list.Count} broken {o.Url} ".ToLogFormat();
        //                    item.Isbroken = true;
        //                }
        //                else
        //                {
        //                    var parsed = Parser.ParseRealty(o, page);
        //                    item.Update(parsed);
        //                    Parser.Log += $"Page {count}/{list.Count} {o.Url} parsed".ToLogFormat();
        //                }
        //                if (string.IsNullOrWhiteSpace(item.Phone))
        //                {
        //                    if (!item.Isbroken)
        //                        Parser.Log += $"Empty phone page {count}/{list.Count} {o.Url} ".ToLogFormat();
        //                    context.Realtys.Remove(item);
        //                }
        //                context.SaveChanges();
        //                DBUpdated();
        //            }
        //        }
        //        catch (Exception)
        //        {

        //        }
        //        if (!Parser.IsRun)
        //            break;
        //    }
        ////}
        public static void ParseAd(bool UseRewrite = false)
        {
            //CreateDB();
            Parser.Log += "Starting parsing/updating all ads.".ToLogFormat();
            List <Realty> list = new List <Realty> {
            };
            object locker      = new object();
            int    ccount      = 0;

            using (var context = new SQLiteDBContext())
            {
                list = context.Realtys.ToList();
            }
            try
            {
                Parallel.ForEach(list, Parser.Options, o =>
                {
                    int count;
                    lock (locker)
                    {
                        ccount++;
                        count = ccount;
                    }
                    try
                    {
                        using (var context = new SQLiteDBContext())
                        {
                            //Parser.Log += $"Parsing {o.Url} page".ToLogFormat();
                            var item = context.Realtys.FirstOrDefault(x => x.Id == o.Id);
                            if (UseRewrite == false && !string.IsNullOrWhiteSpace(item.Phone))
                            {
                                return;
                            }
                            string page = null;
                            while (page == null)
                            {
                                page = Parser.GetPage(o.Url, Parser.ProxyTimeout);
                            }
                            if (page == "skip")
                            {
                                Parser.Log   += $"Page {count}/{list.Count} broken {o.Url} ".ToLogFormat();
                                item.Isbroken = true;
                            }
                            else
                            {
                                var parsed = Parser.ParseRealty(o, page);
                                item.Update(parsed);
                                Parser.Log += $"Page {count}/{list.Count} {o.Url} parsed".ToLogFormat();
                            }
                            if (string.IsNullOrWhiteSpace(item.Phone) || item.Phone == "Empty")
                            {
                                if (!item.Isbroken)
                                {
                                    Parser.Log += $"Empty phone page {count}/{list.Count} {o.Url} ".ToLogFormat();
                                }
                                context.Realtys.Remove(item);
                            }
                            context.SaveChanges();
                            DBUpdated();
                        }
                    }
                    catch (Exception)
                    {
                    }
                });
            }
            catch (OperationCanceledException exc) { }
        }