public static void Insert <T>(this IDbConnection connection,
                                      T entityToInsert,
                                      IDbTransaction transaction = null,
                                      int?commandTimeout         = null) where T : class
        {
            var type     = typeof(T);
            var metadata = MetadataFor(type);

            string sql;

            if (!s_insertQueries.TryGetValue(type.TypeHandle, out sql))
            {
                s_insertQueries[type.TypeHandle] = sql = TSQLGenerator.BuildInsert(metadata, withoutKeys: false);
            }


            Log(sql, entityToInsert);


            connection.Execute(
                sql,
                entityToInsert,
                transaction: transaction,
                commandTimeout: commandTimeout);
        }
Beispiel #2
0
        public void BuildInsert_WithSchema()
        {
            var tableMD = GetTestMetadata();

            tableMD.Schema = "testschema";

            var sql = TSQLGenerator.BuildInsert(tableMD, withoutKeys: false);

            Assert.AreEqual("INSERT INTO [testschema].[testtable] ([a],[b],[c]) VALUES (@aa,@bb,@cc)", sql);
        }
        public static TKey Insert <T, TKey>(this IDbConnection connection,
                                            T entityToInsert,
                                            IDbTransaction transaction = null,
                                            int?commandTimeout         = null,
                                            bool letDbGenerateKey      = false) where T : class
        {
            var type     = typeof(T);
            var metadata = MetadataFor(type);

            var keys = metadata.Properties.Where(x => x.IsPK);

            if (!keys.Any())
            {
                throw new Exception("This only support entites with a single key property at the moment.");
            }
            if (keys.Count() > 1)
            {
                throw new Exception("This only support entites with a single key property at the moment.");
            }

            string sql;

            if (!s_insertQueries.TryGetValue(type.TypeHandle, out sql))
            {
                s_insertQueries[type.TypeHandle] = sql = TSQLGenerator.BuildInsert(metadata, withoutKeys: letDbGenerateKey);
            }

            Log(sql, entityToInsert);

            connection.Execute(
                sql,
                entityToInsert,
                transaction: transaction,
                commandTimeout: commandTimeout);

            if (letDbGenerateKey)
            {
                //NOTE: would prefer to use IDENT_CURRENT('tablename') or IDENT_SCOPE but these are not available on SQLCE
                var r = connection.Query("select @@IDENTITY id", transaction: transaction,
                                         commandTimeout: commandTimeout);
                return((TKey)r.First().id);
            }
            else
            {
                return((TKey)keys.First().PropertyInfo.GetValue(entityToInsert, null));
            }
        }