I am using FluentMigrator to create a new table in DB. After I created, i realized that I need to add a constraint like the following in T-Sql:
Assume I already have a table tableA
Alter Table tableA
Add Constraint ConstraintA CHECK(([ColA]>=(0) AND [ColA]<(100)))
How do I create the constraint using FluentMigrator in .Net? I have googled and did not find any answer. Thanks!
Constraints can be specified when the table is created with the CREATE TABLE statement, or after the table is created with the ALTER TABLE statement.
ADD CONSTRAINT is a SQL command that is used together with ALTER TABLE to add constraints (such as a primary key or foreign key) to an existing table in a SQL database. The basic syntax of ADD CONSTRAINT is: ALTER TABLE table_name ADD CONSTRAINT PRIMARY KEY (col1, col2);
Fluent Migrator is a migration framework for . NET much like Ruby on Rails Migrations. Migrations are a structured way to alter your database schema and are an alternative to creating lots of sql scripts that have to be run manually by every developer involved.
This is a more idiomatic way of doing it in FluentMigrator
Create.UniqueConstraint("SalesIdAndUsername")
.OnTable("users")
.Columns("username", "SalesId");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With