Tables
The Table class in Weasel.MySql.Tables provides a fluent API for defining MySQL tables with columns, primary keys, foreign keys, indexes, and partitioning.
Defining a Table
cs
var table = new Table("users");
table.AddColumn<int>("id").AsPrimaryKey().AutoNumber();
table.AddColumn<string>("name").NotNull();
table.AddColumn<string>("email").NotNull().AddIndex(idx => idx.IsUnique = true);
table.AddColumn<DateTime>("created_at");Table Options
MySQL tables support engine, character set, and collation configuration:
cs
table.Engine = "InnoDB"; // default
table.Charset = "utf8mb4";
table.Collation = "utf8mb4_unicode_ci";Column Configuration
The AddColumn method returns a ColumnExpression with these options:
AsPrimaryKey()-- marks the column as part of the primary keyAutoNumber()-- adds AUTO_INCREMENT to the columnNotNull()/AllowNulls()-- controls nullabilityDefaultValue(value)-- sets a default valueDefaultValueByExpression(expr)-- sets a default using a SQL expressionAddIndex(configure?)-- adds a standard indexAddFulltextIndex(configure?)-- adds a FULLTEXT indexAddSpatialIndex(configure?)-- adds a SPATIAL indexForeignKeyTo(table, column)-- adds a foreign key constraint
Partitioning
MySQL tables support Range, Hash, List, and Key partitioning:
cs
table.PartitionByRange("created_at");
table.PartitionByHash("id");
table.PartitionByList("region");
table.PartitionByKey("id");
table.PartitionCount = 4; // for Hash or Key strategiesDelta Detection
cs
await using var conn = new MySqlConnection(connectionString);
await conn.OpenAsync();
var delta = await table.FindDeltaAsync(conn);Generating DDL
cs
var migrator = new MySqlMigrator();
var writer = new StringWriter();
table.WriteCreateStatement(migrator, writer);The output uses CREATE TABLE IF NOT EXISTS with backtick-quoted identifiers.
