Ejemplo n.º 1
0
        public static SQLServerDbContext RegisterTable <T, TKey>(this SQLServerDbContext context) where T : class, IEntity <TKey>
        {
            string cmdText;
            List <SqlDataColumn> indexes;
            Type type           = typeof(T);
            var  metadataSchema = type.ToMetadataSqlSchema();

            metadataSchema = SqlServerTypeMapping.Map(metadataSchema);
            string conn    = context.ConnectionString;
            var    utility = new SqlUtility <SqlConnection, SqlCommand>(conn);

            if (utility.TableExists(metadataSchema.TableName))
            {
                var existingColumns = getTableColumns(conn, metadataSchema.TableName);
                cmdText = alterTableCommandText(metadataSchema, existingColumns);
                if (!string.IsNullOrEmpty(cmdText))
                {
                    utility.ExecuteSqlCommand(cmdText);
                    indexes = getIndexes(metadataSchema, existingColumns);
                    foreach (var index in indexes)
                    {
                        cmdText = createIndexCommandText(index, metadataSchema.TableName);
                        utility.ExecuteSqlCommand(cmdText);
                    }
                }
            }
            else
            {
                cmdText = createTableCommandText(metadataSchema);
                utility.ExecuteSqlCommand(cmdText);
                indexes = getIndexes(metadataSchema);
                foreach (var index in indexes)
                {
                    cmdText = createIndexCommandText(index, metadataSchema.TableName);
                    utility.ExecuteSqlCommand(cmdText);
                }
            }

            return(context);
        }
Ejemplo n.º 2
0
 public SQLServerRepository(SQLServerDbContext context)
 {
     _typedClient = new TypedClient <TEntity, SqlConnection, TKey>(context.ConnectionString, SQLLanguageType.SQLServer);
 }