public void GraphNewAndExistingEntities()
        {
            using (SakilaLiteUpdateContext context = new SakilaLiteUpdateContext())
            {
                context.InitContext();

                Actor actorNoId, actor;
                context.Attach(actorNoId = new Actor {
                    FirstName = "PENELOPE", LastName = "GUINESS"
                });
                context.Attach(actor = new Actor {
                    ActorId = 21, FirstName = "KIRSTEN", LastName = "PALTROW"
                });

                var changes = context.ChangeTracker.Entries();
                Assert.Equal(2, changes.Count());
                Assert.Collection(changes,
                                  item =>
                {
                    Assert.Equal(EntityState.Added, item.State);
                },
                                  item =>
                {
                    Assert.Equal(EntityState.Unchanged, item.State);
                });

                context.SaveChanges();

                var list = context.Actor.ToList();
                Assert.Equal(1, list.Count);
            }
        }
Example #2
0
        public void GraphNewAndExistingEntities()
        {
            using (SakilaLiteUpdateContext context = new SakilaLiteUpdateContext())
            {
                context.InitContext();

                Actor actorNoId, actor;
                context.Attach(actorNoId = new Actor {
                    FirstName = "PENELOPE", LastName = "GUINESS"
                });
                context.Attach(actor = new Actor {
                    ActorId = 21, FirstName = "KIRSTEN", LastName = "PALTROW"
                });

                var changes = context.ChangeTracker.Entries();
                Assert.AreEqual(2, changes.Count());
                Assert.That(changes.Select(c => c.State), Has.Exactly(1).Matches <EntityState>(state => state.Equals(EntityState.Added)));
                Assert.That(changes.Select(c => c.State), Has.Exactly(1).Matches <EntityState>(state => state.Equals(EntityState.Unchanged)));

                context.SaveChanges();

                var list = context.Actor.ToList();
                Assert.AreEqual(1, list.Count);
            }
        }
        public void StringInterpolationInSqlCommands()
        {
            using (SakilaLiteUpdateContext context = new SakilaLiteUpdateContext())
            {
                context.InitContext();

                int      id         = 1;
                string   firstName  = "PENELOPE";
                string   lastName   = "GUINESS";
                DateTime lastUpdate = DateTime.Parse("2006-02-15 04:34:33");
                context.Database.ExecuteSqlCommand($"INSERT INTO actor(actor_id, first_name, last_name, last_update) VALUES ({id}, {firstName}, {lastName}, {lastUpdate:u})");
                Actor actor = context.Set <Actor>().FromSql($"SELECT * FROM actor WHERE actor_id={id} and last_name={lastName}").First();

                Assert.Equal(id, actor.ActorId);
                Assert.Equal(firstName, actor.FirstName);
            }
        }
Example #4
0
        public void TransactionScopeTest()
        {
            using (var context = new SakilaLiteUpdateContext())
            {
                context.InitContext(false);
            }

            using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required,
                                                                 new TransactionOptions {
                IsolationLevel = IsolationLevel.ReadCommitted
            }))
            {
                using (MySqlConnection connection = new MySqlConnection(MySQLTestStore.GetContextConnectionString <SakilaLiteUpdateContext>()))
                {
                    connection.Open();

                    MySqlCommand command = connection.CreateCommand();
                    command.CommandText = "DELETE FROM actor";
                    command.ExecuteNonQuery();

                    var options = new DbContextOptionsBuilder <SakilaLiteUpdateContext>()
                                  .UseMySQL(connection)
                                  .Options;

                    using (TransactionScope innerScope = new TransactionScope(TransactionScopeOption.Required))
                    {
                        using (var context = new SakilaLiteUpdateContext(options))
                        {
                            context.Actor.Add(new Actor
                            {
                                FirstName = "PENELOPE",
                                LastName  = "GUINESS"
                            });
                            context.SaveChanges();
                            innerScope.Complete();
                        }
                    }

                    // Commit transaction if all commands succeed, transaction will auto-rollback
                    // when disposed if either commands fails
                    scope.Complete();
                }
            }
        }
Example #5
0
        public void TransactionTest()
        {
            using (var context = new SakilaLiteUpdateContext())
            {
                context.InitContext(false);
                MySqlTrace.LogInformation(9966, "EF Model CREATED");
            }

            using (MySqlConnection connection = new MySqlConnection(MySQLTestStore.GetContextConnectionString <SakilaLiteUpdateContext>()))
            {
                connection.Open();

                using (MySqlTransaction transaction = connection.BeginTransaction())
                {
                    MySqlCommand command = connection.CreateCommand();
                    command.CommandText = "DELETE FROM actor";
                    command.ExecuteNonQuery();

                    var options = new DbContextOptionsBuilder <SakilaLiteUpdateContext>()
                                  .UseMySQL(connection)
                                  .Options;

                    using (var context = new SakilaLiteUpdateContext(options))
                    {
                        context.Database.UseTransaction(transaction);
                        context.Actor.Add(new Actor
                        {
                            FirstName = "PENELOPE",
                            LastName  = "GUINESS"
                        });
                        context.SaveChanges();
                    }

                    transaction.Commit();
                }
            }
        }