public void IdentityInsert()
        {
            Assert.That(() =>
            {
                using (var transaction = new TransactionScope())
                {
                    using (DbReposetoryIdentityInsertScope.CreateOrObtain())
                    {
                        var image     = new Image();
                        image.ImageId = 10;
                        _images.Add(image);
                        var book    = new Book();
                        book.BookId = 0;
                        _books.Add(book);
                        image.IdBook = book.BookId;

                        Assert.That(book.BookId, Is.EqualTo(0));
                        Assert.That(image.ImageId, Is.EqualTo(10));

                        Assert.AreNotEqual(_books.Count, 0);
                        Assert.AreNotEqual(_images.Count, 0);
                        transaction.Complete();
                    }
                }
            }, Throws.Nothing);
        }
        public void ParentWithNullableChildIsNullWithPkInsert()
        {
            Assert.That(() =>
            {
                using (var transaction = new TransactionScope())
                {
                    var completedFlag = false;
                    using (var scope = DbReposetoryIdentityInsertScope.CreateOrObtain())
                    {
                        var book  = new Book();
                        var image = new ImageNullable();

                        Assert.That(() => _books.Add(book), Throws.Nothing);
                        image.IdBook = null;
                        Assert.That(() => _imagesNullable.Add(image), Throws.Nothing);
                        scope.OnIdentityInsertCompleted += (sender, args) =>
                        {
                            Assert.That(completedFlag, Is.False);
                            completedFlag = true;
                        };
                        Assert.That(completedFlag, Is.False);
                        transaction.Complete();
                    }
                    Assert.That(completedFlag, Is.True);
                }
            }, Throws.Nothing);
        }
 public void TestInvalidReplicationScope_WithoutTransaction()
 {
     Assert.That(() =>
     {
         using (var identityInsertScope = DbReposetoryIdentityInsertScope.CreateOrObtain())
         {
         }
     }, Throws.Exception.TypeOf <InvalidOperationException>());
 }
Exemplo n.º 4
0
 public void CreateRole(RoleEntity roleEntity)
 {
     using (new TransactionScope())
     {
         using (DbReposetoryIdentityInsertScope.CreateOrObtain())
         {
             Roles.Add(roleEntity);
         }
     }
 }
Exemplo n.º 5
0
 public void InsertIdentityOnWrongScope()
 {
     //ThreadConnectionController.UseTransactionClass();
     DbAccess.Database.RunInTransaction(f =>
     {
         Assert.That(() =>
         {
             using (DbReposetoryIdentityInsertScope.CreateOrObtain())
             {
             }
         }, Throws.Exception);
     });
 }
Exemplo n.º 6
0
 public void InsertIdentity()
 {
     Assert.That(() => DbAccess.Database.RunInTransaction(f =>
     {
         using (DbReposetoryIdentityInsertScope.CreateOrObtain())
         {
             DbAccess.Insert(new Users()
             {
                 UserID = 999999, UserName = "******"
             });
             DbAccess.Query().Update.Table <Users>().Set.Column(g => g.UserID).Value(666).ExecuteNonQuery();
         }
     }), Throws.Exception);
 }
 public void TestInvalidReplicationScope_Nested()
 {
     Assert.That(() =>
     {
         using (var scope = new TransactionScope())
         {
             using (var identityInsertScope = DbReposetoryIdentityInsertScope.CreateOrObtain())
             {
                 using (var insertScope = DbReposetoryIdentityInsertScope.CreateOrObtain())
                 {
                 }
             }
         }
     }, Throws.Nothing);
 }
 private void _currentTransaction_TransactionCompleted(object sender, TransactionEventArgs e)
 {
     lock (LockRoot)
     {
         _currentTransaction = null;
         _currentDbReposetoryIdentityInsertScope = null;
         if (e.Transaction.TransactionInformation.Status == TransactionStatus.Aborted)
         {
             _currentTransaction_Rollback();
         }
         else
         {
             _currentTransaction_TransactionCompleted();
         }
     }
 }
        private object SetNextId(object item)
        {
            var idVal = GetId(item);

            if (DbReposetoryIdentityInsertScope.Current != null && _currentDbReposetoryIdentityInsertScope == null)
            {
                _currentDbReposetoryIdentityInsertScope = DbReposetoryIdentityInsertScope.Current;
                _currentDbReposetoryIdentityInsertScope.EnsureTransaction();
            }

            if (_currentDbReposetoryIdentityInsertScope != null)
            {
                if (idVal.Equals(Constraints.PrimaryKey.GetUninitilized()) && !_currentDbReposetoryIdentityInsertScope.RewriteDefaultValues)
                {
                    return(idVal);
                }
                if (!idVal.Equals(Constraints.PrimaryKey.GetUninitilized()))
                {
                    return(idVal);
                }
                lock (LockRoot)
                {
                    var newId = Constraints.PrimaryKey.GetNextValue();
                    _typeInfo.PrimaryKeyProperty.Setter.Invoke(item,
                                                               Convert.ChangeType(newId, _typeInfo.PrimaryKeyProperty.PropertyType));
                    return(newId);
                }
            }

            if (idVal.Equals(Constraints.PrimaryKey.GetUninitilized()))
            {
                lock (LockRoot)
                {
                    var newId = Constraints.PrimaryKey.GetNextValue();
                    _typeInfo.PrimaryKeyProperty.Setter.Invoke(item,
                                                               Convert.ChangeType(newId, _typeInfo.PrimaryKeyProperty.PropertyType));
                    return(newId);
                }
            }

            var exception =
                new InvalidOperationException(string.Format("Cannot insert explicit value for identity column in table '{0}' " +
                                                            "when no IdentityInsertScope exists.", _typeInfo.Name));

            throw exception;
        }
Exemplo n.º 10
0
        public void ParentWithNullableChildWithPkInsert()
        {
            Assert.That(() =>
            {
                using (var transaction = new TransactionScope())
                {
                    using (DbReposetoryIdentityInsertScope.CreateOrObtain())
                    {
                        var book  = new Book();
                        var image = new ImageNullable();

                        Assert.That(() => _books.Add(book), Throws.Nothing);
                        image.IdBook = book.BookId;
                        Assert.That(() => _imagesNullable.Add(image), Throws.Nothing);
                        transaction.Complete();
                    }
                }
            }, Throws.Nothing);
        }
Exemplo n.º 11
0
        public void InsertIOReaddTriggerOrder()
        {
            var     orderFlag = false;
            var     inserted  = false;
            DbScope repro;

            using (repro = MockRepro())
            {
                repro.users.Triggers.WithReplication.For.Insert += (sender, token) =>
                {
                    if (!inserted)
                    {
                        Assert.That(orderFlag, Is.False);
                    }
                    orderFlag = true;
                };

                repro.users.Triggers.WithReplication.After.Insert += (sender, token) =>
                {
                    Assert.That(orderFlag, Is.True);
                };

                repro.users.Triggers.WithReplication.InsteadOf.Insert += (sender, token) =>
                {
                    Assert.That(orderFlag, Is.True);
                    inserted = true;
                    using (var tr = new TransactionScope())
                    {
                        using (DbReposetoryIdentityInsertScope.CreateOrObtain())
                        {
                            token.Table.Add(token.Item);
                        }
                        tr.Complete();
                    }
                };
            }

            Assert.That(orderFlag, Is.False);
            repro.users.Add(new Users());
            Assert.That(orderFlag, Is.True);
            Assert.That(repro.users.Count, Is.EqualTo(1));
        }
Exemplo n.º 12
0
        /// <summary>
        ///     Reads the XML.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <exception cref="InvalidDataException">
        ///     Invalid XML document for Db import. index is unset
        ///     or
        ///     Invalid XML document for Db import. type is unset
        ///     or
        ///     or
        ///     Invalid XML document for Db import. index for a table is unset
        /// </exception>
        public void ReadXml(XmlReader reader)
        {
            reader.Read();
            reader.ReadStartElement(DatabaseName);
            reader.ReadStartElement(ReprosIncluded);

            var elements = new Dictionary <string, Type>();

            do
            {
                var indexOfElementString = reader.GetAttribute("Index");
                var typeString           = reader.GetAttribute("Type");
                if (indexOfElementString == null)
                {
                    throw new InvalidDataException("Invalid XML document for Db import. index is unset");
                }

                if (typeString == null)
                {
                    throw new InvalidDataException("Invalid XML document for Db import. type is unset");
                }

                var type = Type.GetType(typeString);

                if (type == null)
                {
                    throw new InvalidDataException(string.Format("Invalid XML document for Db import. type is invalid '{0}'",
                                                                 typeString));
                }

                elements.Add(indexOfElementString, type);
            } while (reader.Read() && reader.Name == ReproIncluded);
            reader.ReadEndElement();
            reader.ReadStartElement(DatabaseContent);
            using (var transaction = new TransactionScope())
            {
                using (DbReposetoryIdentityInsertScope.CreateOrObtain())
                {
                    do
                    {
                        var indexOfElementString = reader.GetAttribute("Index");
                        if (indexOfElementString == null)
                        {
                            throw new InvalidDataException("Invalid XML document for Db import. index for a table is unset");
                        }
                        reader.ReadStartElement(TableContentList);
                        var type  = elements[indexOfElementString];
                        var table =
                            LocalDbManager.Scope.Database.First(s => s.Key.AssemblyQualifiedName == type.AssemblyQualifiedName).Value;
                        if (table == null)
                        {
                            throw new InvalidDataException("Invalid Database config for Db import. There is no Table for the type " + type);
                        }
                        reader.ReadStartElement(TableContentElementsList);
                        do
                        {
                            object emptyElement = table.TypeInfo.DefaultFactory();
                            reader.ReadStartElement(table.TypeInfo.TableName);
                            do
                            {
                                var propName = reader.Name;
                                var isNumm   = reader.IsEmptyElement;

                                var value = reader.ReadElementContentAsString();
                                var dbPropertyInfoCache = table.TypeInfo.Propertys[table.TypeInfo.SchemaMappingDatabaseToLocal(propName)];

                                object contvertedValue = null;
                                if (!isNumm)
                                {
                                    contvertedValue = DataConverterExtensions.ChangeType(value, dbPropertyInfoCache.PropertyType);
                                }

                                dbPropertyInfoCache.Setter.Invoke(emptyElement, contvertedValue);
                            } while (reader.Name != table.TypeInfo.TableName);
                            reader.ReadEndElement();

                            table.Add(emptyElement);
                        } while (reader.Name == table.TypeInfo.TableName);

                        reader.ReadEndElement();
                        //for newer indexer tags. Currently unsupported
                        if (reader.Name == IndexerIncluded)
                        {
                            reader.ReadStartElement(IndexerIncluded);
                            reader.ReadEndElement();
                        }
                    } while (reader.Name != DatabaseContent);
                    transaction.Complete();
                }
            }
        }