[英]EF5 database migrations: How to Enable FILESTREAMS
我正在研究应用程序一段时间,我们需要添加一个迁移到其他东西上,以便在SQL Server上启用FILESTREAM。 我的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方法还可以。
非常感谢。
我终于有了一个解决方案,我认为无法使用脚本在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]");
}
这对我来说还可以。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.