EF5数据库迁移:如何启用FILESTREAMS

时间:2013-11-18 18:52:24

标签: sql entity-framework filestream database-migration

我正在开发一段时间的应用程序,我们需要在SQL服务器上添加迁移以及其他启用FILESTREAM的功能。 My Up()方法如下所示:

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]");
     }

我需要一些sql脚本来向该组添加文件组和文件(该文件必须与我的数据库文件位于同一路径上)并且我还需要知道我已经添加的脚本是否正常以及是否类型我穿上c.Binary方法还可以。

提前多多感谢。

1 个答案:

答案 0 :(得分:1)

我终于有了解决方案,我认为没有办法在Windows级别使用脚本启用文件流,但可以使用此代码实现其余配置。希望它对某些人有所帮助。

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]");
 }

这对我有用。