简体   繁体   中英

EF5 database migrations: How to Enable FILESTREAMS

I'm working on an Application for a while now and we have the need to add a migration to among other things enable FILESTREAM on SQL server. My Up() method looks like this:

public override void Up()
    {
        //Enable filestream
        Sql("USE master " +
            "Go " +
            "EXEC sp_configure 'show advanced options' " +
            "GO " +
            "EXEC sp_configure filestream_access_level, 1 " +
            "GO " +
            "RECONFIGURE WITH OVERRIDE " +
            "GO");
        //Need to add some script here to create FILEGROUP and add a file to that
        //file group to be used by FILESTREAM
        CreateTable(
            "dbo.PatientAttachmentEntities",
            c => new
                {
                    Id = c.Guid(nullable: false),
                    PatientMedicalDataId = c.Guid(nullable: false),
                    FileName = c.String(nullable: false),
                    FileDescription = c.String(),
                })
            .PrimaryKey(t => t.Id)
            .ForeignKey("dbo.PatientMedicalDataEntities", t => t.PatientMedicalDataId, cascadeDelete: true)
            .ForeignKey("dbo.AttachmentContentEntities", t => t.Id)
            .Index(t => t.PatientMedicalDataId)
            .Index(t => t.Id);

   //CreateTable(
   //         "dbo.AttachmentContentEntities",
   //         c => new
   //             {
   //                 Id = c.Guid(nullable: false),
   //                 //Need to know if the following line is ok to setup a FILESTREAM column
   //                 Content = c.Binary(storeType:"varbinary(max) FILESTREAM", nullable:true),
   //             })
   //         .PrimaryKey(t => t.Id);
   Sql("CREATE TABLE [dbo].[AttachmentContentEntities]( " +
       "[Id] [uniqueidentifier] ROWGUIDCOL NOT NULL PRIMARY KEY, " +
       "[Content] [varbinary](max) FILESTREAM  NOT NULL ) " +
       "ON [PRIMARY] FILESTREAM_ON [MEDIC_FS]");
     }

I need some sql script to add a filegroup and a file to that group (the file must be on the same path than my database file) and also I need to know if the script I already add is ok and if the type I put on the c.Binary method are Ok.

Thanks a lot in advance.

I've finally have a solution for this, I think there is no way to enable filestream at windows level using scripts but the rest of the configuration can be achived using this code. Hope it could be of some help for some one out there.

public override void Up()
{
    //File Stream
        Sql("USE master " +
            "EXEC sp_configure 'show advanced options' " +
            "EXEC sp_configure filestream_access_level, 1 " +
            "RECONFIGURE WITH OVERRIDE " +
            "ALTER DATABASE Medic " +
            "ADD FILEGROUP MEDIC_FS " +
            "CONTAINS FILESTREAM " +
            "DECLARE @db_path NVARCHAR(MAX) " +
            "SELECT @db_path = physical_name FROM sys.master_files WHERE database_id = DB_ID(N'Medic') AND type_desc = 'ROWS' " +
            @"SET @db_path = REVERSE(RIGHT(REVERSE(@db_path),(LEN(@db_path)-CHARINDEX('\', REVERSE(@db_path),1))+1)) + 'MEDIC_FS' " +
            "DECLARE @Sql NVARCHAR(MAX) " +
            "SET @Sql = 'ALTER DATABASE Medic ADD FILE ( NAME= ''Medic_FS'', FILENAME = ' + QuoteName(@db_path,'''') + ' ) TO FILEGROUP MEDIC_FS' " +
            "EXEC( @Sql ) ", suppressTransaction: true);        
    CreateTable(
        "dbo.PatientAttachmentEntities",
        c => new
            {
                Id = c.Guid(nullable: false),
                PatientMedicalDataId = c.Guid(nullable: false),
                FileName = c.String(nullable: false),
                FileDescription = c.String(),
            })
        .PrimaryKey(t => t.Id)
        .ForeignKey("dbo.PatientMedicalDataEntities", t => t.PatientMedicalDataId, cascadeDelete: true)
        .ForeignKey("dbo.AttachmentContentEntities", t => t.Id)
        .Index(t => t.PatientMedicalDataId)
        .Index(t => t.Id);

  Sql("CREATE TABLE [dbo].[AttachmentContentEntities]( " +
   "[Id] [uniqueidentifier] ROWGUIDCOL NOT NULL PRIMARY KEY, " +
   "[Content] [varbinary](max) FILESTREAM  NOT NULL ) " +
   "ON [PRIMARY] FILESTREAM_ON [MEDIC_FS]");
 }

This is working ok for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM