[英]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.