Exemplo n.º 1
0
        public void UpdateSimple()
        {
            var sb = new MySqlConnectionStringBuilder(ConnectionString);

            sb.Logging = true;
            using (DefaultContext ctx = new DefaultContext(sb.ToString()))
            {
                MySqlTrace.Listeners.Clear();
                MySqlTrace.Switch.Level = SourceLevels.All;
                GenericListener listener = new GenericListener();
                MySqlTrace.Listeners.Add(listener);

                Product p = new Product()
                {
                    Name = "Acme"
                };
                ctx.Products.Add(p);
                ctx.SaveChanges();

                p.Name = "Acme 2";
                ctx.SaveChanges();

                Regex rx = new Regex(@"Query Opened: (?<item>UPDATE .*)", RegexOptions.Compiled | RegexOptions.Singleline);
                foreach (string s in listener.Strings)
                {
                    Match m = rx.Match(s);
                    if (m.Success)
                    {
                        CheckSqlContains(m.Groups["item"].Value,
                                         @"UPDATE `Products` SET `Name`='Acme 2' WHERE `Id` = 1;
                SELECT `CreatedDate` FROM `Products` WHERE  row_count() = 1 and (`Id` = 1)");
                    }
                }
            }
        }
Exemplo n.º 2
0
        public void DoubleValuesNonEnglish()
        {
            CultureInfo curCulture   = Thread.CurrentThread.CurrentCulture;
            CultureInfo curUICulture = Thread.CurrentThread.CurrentUICulture;
            CultureInfo newCulture   = new CultureInfo("da-DK");

            Thread.CurrentThread.CurrentCulture   = newCulture;
            Thread.CurrentThread.CurrentUICulture = newCulture;

            try
            {
                using (DefaultContext ctx = GetDefaultContext())
                {
                    Product p = new Product();
                    p.Name        = "New Product";
                    p.Weight      = 8.65f;
                    p.CreatedDate = DateTime.Now;
                    ctx.Products.Add(p);
                    ctx.SaveChanges();
                }
            }
            finally
            {
                Thread.CurrentThread.CurrentCulture   = curCulture;
                Thread.CurrentThread.CurrentUICulture = curUICulture;
            }
        }
Exemplo n.º 3
0
        public void InsertSingleRow()
        {
            using (DefaultContext ctx = new DefaultContext(ConnectionString))
            {
                int beforeCnt = ctx.Companies.Count();

                Company c = new Company();
                c.Name            = "Yoyo";
                c.NumEmployees    = 486;
                c.DateBegan       = DateTime.Now;
                c.Address         = new Address();
                c.Address.Street  = "212 My Street.";
                c.Address.City    = "Helena";
                c.Address.State   = "MT";
                c.Address.ZipCode = "44558";

                ctx.Companies.Add(c);
                int result = ctx.SaveChanges();

                Assert.AreEqual(beforeCnt + 1, ctx.Companies.Count());

                Company d = ctx.Companies.Find(c.Id);
                d.Id = c.Id;
                Assert.AreEqual(c, d);
            }
        }
Exemplo n.º 4
0
 void LoadData()
 {
   using (DefaultContext ctx = new DefaultContext(st.ConnectionString))
   {
     ctx.Products.Add(new Product() { Name = "Garbage Truck", MinAge = 8 });
     ctx.Products.Add(new Product() { Name = "Fire Truck", MinAge = 12 });
     ctx.Products.Add(new Product() { Name = "Hula Hoop", MinAge = 18 });
     ctx.SaveChanges();
   }
 }
Exemplo n.º 5
0
        public void TimestampColumn()
        {
            DateTime now = DateTime.Now;

            using (DefaultContext ctx = GetDefaultContext())
            {
                Product p = new Product()
                {
                    Name = "My Product", MinAge = 7, Weight = 8.0f
                };
                ctx.Products.Add(p);
                ctx.SaveChanges();

                p             = ctx.Products.First();
                p.CreatedDate = now;
                ctx.SaveChanges();

                p = ctx.Products.First();
                Assert.AreEqual(now, p.CreatedDate);
            }
        }
Exemplo n.º 6
0
    public void SimpleDeleteRowByParameter()
    {
      using (DefaultContext ctx = new DefaultContext(st.ConnectionString))
      {
        int total = ctx.Products.Count();
        int cntLeft = ctx.Products.Where(b => b.MinAge >= 18).Count();
        // make sure the test is valid
        Assert.True(total > cntLeft);

        foreach (Product p in ctx.Products.Where(b => b.MinAge < 18).ToList())
          ctx.Products.Remove(p);
        ctx.SaveChanges();
        Assert.Equal(cntLeft, ctx.Products.Count());
        st.NeedSetup = true;
      }
    }
Exemplo n.º 7
0
    public void SimpleDeleteAllRows()
    {
      using (DefaultContext ctx = new DefaultContext(st.ConnectionString))
      {
        Assert.True(ctx.Products.Count() > 0);

        foreach (Product p in ctx.Products)
          ctx.Products.Remove(p);
        ctx.SaveChanges();

        Assert.Equal(0, ctx.Products.Count());
      }
      // set the flag that will cause the setup to happen again
      // since we just blew away a table
      st.NeedSetup = true;
    }
Exemplo n.º 8
0
        public void TimeType()
        {
            using (DefaultContext ctx = GetDefaultContext())
            {
                TimeSpan birth = new TimeSpan(11, 3, 2);

                Child c = new Child();
                c.ChildId   = "ABC";
                c.Name      = "first";
                c.BirthTime = birth;
                c.Label     = Guid.NewGuid();
                ctx.Children.Add(c);
                ctx.SaveChanges();

                Child d = ctx.Children.Where(x => x.ChildId == "ABC").Single();
                Assert.AreEqual(birth, d.BirthTime);
            }
        }
 public override void LoadData()
 {
     using (DefaultContext ctx = new DefaultContext(ConnectionString))
     {
         ctx.Products.Add(new Product()
         {
             Id = 1, Name = "Garbage Truck", Weight = 8.865f
         });
         ctx.Products.Add(new Product()
         {
             Id = 2, Name = "Fire Truck", Weight = 12.623f
         });
         ctx.Products.Add(new Product()
         {
             Id = 3, Name = "Hula Hoop", Weight = 2.687f
         });
         ctx.SaveChanges();
     }
 }
Exemplo n.º 10
0
        public void GuidType()
        {
            using (DefaultContext ctx = st.GetDefaultContext())
            {
                TimeSpan birth = new TimeSpan(11, 3, 2);
                Guid     g     = Guid.NewGuid();

                Child c = new Child();
                c.ChildId   = "GUID";
                c.Name      = "first";
                c.BirthTime = birth;
                c.Label     = g;
                ctx.Children.Add(c);
                ctx.SaveChanges();

                Child d = ctx.Children.Where(x => x.ChildId == "GUID").Single();
                Assert.Equal(g, d.Label);
            }
        }
Exemplo n.º 11
0
        public void SimpleDeleteRowByParameter()
        {
            using (DefaultContext ctx = new DefaultContext(ConnectionString))
            {
                int total   = ctx.Products.Count();
                int cntLeft = ctx.Products.Where(b => b.MinAge >= 18).Count();
                // make sure the test is valid
                Assert.True(total > cntLeft);

                foreach (Product p in ctx.Products.Where(b => b.MinAge < 18).ToList())
                {
                    ctx.Products.Remove(p);
                }
                ctx.SaveChanges();
                Assert.AreEqual(cntLeft, ctx.Products.Count());
            }
            // set the flag that will cause the setup to happen again
            // since we just blew away a table
            NeedSetup = true;
        }