Exemplo n.º 1
0
        public void InsertGuidPrimaryKey()
        {
            if (CI.IsTravis)
            {
                // Don't run SQL Server test on Linux
                return;
            }

            using (var con = new SqlServerDatabaseDriver().GetConnection())
            {
                con.Execute("CREATE TABLE dbo.Bazs (Id UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID(), Name VARCHAR(255));");

                object identity;
                try
                {
                    DommelMapper.AddSqlBuilder(typeof(SqlConnection), new GuidSqlServerSqlBuilder());
                    identity = con.Insert(new Baz {
                        Name = "blah"
                    });
                }
                finally
                {
                    DommelMapper.AddSqlBuilder(typeof(SqlConnection), new DommelMapper.SqlServerSqlBuilder());
                }

                Assert.NotNull(identity);
                var id  = Assert.IsType <Guid>(identity);
                var baz = con.Get <Baz>(id);
                Assert.NotNull(baz);
                Assert.Equal("blah", baz.Name);
                Assert.Equal(id, baz.Id);
                con.Execute("DROP TABLE dbo.Bazs");
            }
        }
        public async Task InsertGuidPrimaryKey()
        {
            if (CI.IsTravis)
            {
                // Don't run SQL Server test on Linux
                return;
            }

            using var con = new SqlServerDatabaseDriver().GetConnection();
            await con.ExecuteAsync("CREATE TABLE dbo.Quxs (Id UNIQUEIDENTIFIER NOT NULL DEFAULT NEWID(), Name VARCHAR(255));");

            try
            {
                object identity;
                try
                {
                    DommelMapper.AddSqlBuilder(typeof(SqlConnection), new GuidSqlServerSqlBuilder());
                    identity = await con.InsertAsync(new Qux { Name = "blah" });
                }
                finally
                {
                    DommelMapper.AddSqlBuilder(typeof(SqlConnection), new SqlServerSqlBuilder());
                }

                Assert.NotNull(identity);
                var id  = Assert.IsType <Guid>(identity);
                var baz = await con.GetAsync <Qux>(id);

                Assert.NotNull(baz);
                Assert.Equal("blah", baz.Name);
                Assert.Equal(id, baz.Id);
            }
            finally
            {
                await con.ExecuteAsync("DROP TABLE dbo.Quxs");
            }
        }