databaseentity-frameworkef-code-firstmigrationsqlfilestream

Change database options during EF migration


We have some POCO classes and migrations enabled for my dataaccess layer we have created a initial migration to - remark we use the CreateDatabaseIfNotExist db initialization.

But in the database I would like have a MessageBody field that uses SQL Filestream, because the limitation on EF for Filestream - we try to do it manually in the migration script.

There we execute the following sql command.

Sql("alter table [msg].[Message] add [MessageBody] varbinary(max) FILESTREAM not null");

But I have to set the filestream options Filestream options

So I would like to execute the following command during / before the migration.

ALTER DATABASE CURRENT SET FILESTREAM ( NON_TRANSACTED_ACCESS = FULL)

But when adding this bedore the creation of the tables I get the error: ALTER DATABASE statement not allowed within multi-statement transaction.

What's the best way to adapt Database options when you want to create the database automatically? Is it possible to intercept the migration process to execute some sql before the process executes the acutal migrations?


Solution

  • To fix error that occured to you you only need to invoke Sql method with additional bool parameter set to true:

    Sql("alter table [msg].[Message] add [MessageBody] varbinary(max) FILESTREAM not null", true);
    

    This will cause that your query will be executed in separate transaction.