Fluent SQL

CREATE VIEW

To create a view, call View.Create(). Next call As() with the SelectStatement that defines the view. The SELECT statement for the view is built up the same way as a standalone SELECT statement.

var createStatement = View.Create("MyCustomerView")
    .As(
        Select
            .All("ID", "Name", "LastUpdated")
            .From("Customer")
    );
CREATE VIEW [MyCustomerView]
AS
SELECT [ID], [Name], [LastUpdated]
FROM [Customer];

If you don’t call As() with a SELECT statement, SQL generation will generate an error.

-- SQL Error: Create view statement must have a select expression.

View attributes

There are a few attributes that you can add when creating views. The ENCRYPTION, SCHEMABINDING, and VIEW_METADATA options are described here: T-SQL | CREATE VIEW.

You can add these attributes by chaining the methods WithEncryption(), WithSchemaBinding(), and WithViewMetadata().

var createStatement = View.Create("MyCustomerView")
    .WithSchemaBinding()
    .As(
        Select
            .All("ID", "Name", "LastUpdated")
            .From("Customer")
    );
CREATE VIEW [MyCustomerView]
WITH SCHEMABINDING
AS
SELECT [ID], [Name], [LastUpdated]
FROM [Customer];