问题
I did some searching and I could not find very much on utilizing filestream with OrmLite. I think it is possible but I am not sure which direction to take.
Ideally I would like to be able to create or drop/create a table based on a model with a binary field and then do something to make that column in the database mapped to the filestream. I know that the filestream has to be setup on sql server ahead of time (I don't think you can do ALL the filestream setup from outside of the Management Studio / Configuration Manager)
Is there a way to do this using OrmLite? I did find this part:
db.ExecuteNonQuery("UPDATE Person SET LastName=@name WHERE Id=@id", new { name = "WaterHouse", id = 7 });
And:
CREATE TABLE Archive.dbo.Records ( [Id] [uniqueidentifier] ROWGUIDCOL NOT NULL UNIQUE, [SerialNumber] INTEGER UNIQUE, [Chart] VARBINARY(MAX) FILESTREAM NULL ) GO
ALTER TABLE might work but I can't figure out how to combine modifying just one column using SQL in OrmLite or Ormlite on its own.
回答1:
This isn't quite possible out of the box with OrmLite, but you can write an extension method that uses the OrmLite base to do what you want (code below is v3). This method simply drops all of the varbinary columns and re-adds them as filestreams:
public static class OrmLiteBinaryCreateExtensions
{
/// <summary>
/// WARNING: this will drop all of the existing varbinary columns!
/// </summary>
public static void UpdateByteToBinary<T>(this IDbConnection dbConn)
{
var modelDef = ModelDefinition<T>.Definition;
var tableName = OrmLiteConfig.DialectProvider.GetQuotedTableName(modelDef);
var definitions = modelDef.FieldDefinitions.Where<FieldDefinition>(f => f.FieldType == typeof(byte[]));
foreach (var def in definitions)
{
var columnName = OrmLiteConfig.DialectProvider.GetQuotedColumnName(def.FieldName);
dbConn.ExecuteNonQuery(string.Format("ALTER TABLE {0} DROP COLUMN {1}", tableName, columnName));
dbConn.ExecuteNonQuery(string.Format("ALTER TABLE {0} ADD {1} [varbinary](max) filestream NULL", tableName, columnName));
}
}
}
Usage:
using (IDbConnection db = dbFactory.OpenDbConnection())
{
// create temp file, read bytes, delete it
var tmp = Path.GetTempFileName();
using (var fs = new FileStream(tmp, FileMode.OpenOrCreate))
{
var textBytes = System.Text.Encoding.UTF8.GetBytes("some text");
fs.Write(textBytes, 0, textBytes.Length);
}
byte[] bytes = File.ReadAllBytes(tmp);
File.Delete(tmp);
// stand up data in orm lite
db.CreateTableIfNotExists<FileExample>();
// here is our extension method - note that this will drop the entire file column
// and lose all existing data
db.UpdateByteToBinary<FileExample>();
db.Insert<FileExample>(new FileExample { Name = "my new file", FileBytes = bytes });
// read data back to ensure it saved properly
var files = db.Select<FileExample>();
using (var ms = new MemoryStream(files[0].FileBytes))
{
var someText = System.Text.Encoding.UTF8.GetString(files[0].FileBytes);
Console.WriteLine(someText);
}
db.DropTable<FileExample>();
}
来源:https://stackoverflow.com/questions/20535267/ormlite-with-filestream