Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Constraint to Table column when using Create.Table, FluentMigrator

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!

like image 525
AlliceSmash Avatar asked Jun 19 '14 15:06

AlliceSmash


People also ask

Can constraints be created after table creation?

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.

How can constraints can be added on a table?

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);

What is fluent Migrator?

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.


1 Answers

This is a more idiomatic way of doing it in FluentMigrator

Create.UniqueConstraint("SalesIdAndUsername")
  .OnTable("users")
  .Columns("username", "SalesId");
like image 59
Karaaie Avatar answered Sep 27 '22 20:09

Karaaie