[英]FluentNHibernate mapping settings - file storage in SQL Server database
I've read various things but I didn't see something specific, so I'm reposting. 我读过各种各样的东西,但我没有看到具体的东西,所以我要重新发布。 Sorry if I missed one and duplicate posted. 对不起,如果我错过了一个并重复发布。
I am storing files in a database; 我将文件存储在数据库中; previously, with ADO.NET Entity Framework, I would use image type and it streams it as byte[] array. 以前,使用ADO.NET Entity Framework,我会使用图像类型,并将其作为byte []数组进行流式处理。
Is that the approach to do it in NHibernate with FluentNHibernate mappings? 这是使用FluentNHibernate映射在NHibernate中进行的方法吗? I setup the column as image. 我将列设置为图像。 I defined this as the mapping for the property (which the C# property is a byte[] array): 我将其定义为属性的映射(C#属性是byte []数组):
Map(i => i.FileContents).CustomSqlType("image");
Is that the correct way to set this up? 这是正确的设置方法吗? I am getting an error and am not sure if its related to this? 我收到一个错误,我不确定它是否与此相关?
Thanks. 谢谢。
You don't need a custom type. 您不需要自定义类型。 Here is a mapping that works for a SQL Server image column named Content: 这是一个适用于名为Content的SQL Server图像列的映射:
Map(x => x.Content);
Here is usage of that mapping: 以下是该映射的用法:
byte[] content = nhSession.CreateCriteria<AttachmentContent>()
.Add<AttachmentContent>(ac => ac.Id == 3)
.SetProjection(Projections.Property("Content"))
.UniqueResult<byte[]>();
...and here's a way to get it out without a mapping (AttachmentDTO is not a mapped NH class, just a normal class) : ...这里有一种没有映射的方法(AttachmentDTO不是映射的NH类,只是一个普通的类):
nhSession.CreateSQLQuery("select a.Content from Attachments a where a.Id = 1")
.SetResultTransformer(Transformers.AliasToBean(typeof(AttachmentDTO)))
.UniqueResult<AttachmentDTO>();
Here's the DTO class: 这是DTO课程:
public class AttachmentDTO {
public int Id { get; set; }
public string ContentType { get; set; }
public byte[] Content { get; set; }
}
Good luck! 祝好运!
You can also map Custom<TType>
s to NHibernate.Type
types 您还可以将Custom<TType>
s映射到NHibernate.Type
类型
For instance: 例如:
Map(i => i.FileContents).Custom<NHibernate.Type.BinaryBlobType>();
I've mapped files stored as binary, but they weren't the 'image' type. 我已将映射的文件映射为二进制文件,但它们不是“图像”类型。
I did a quick google search and found a post with an ImageUserType which you could try to specify instead. 我做了一个快速谷歌搜索,发现了一个ImageUserType的帖子,您可以尝试指定。 http://weblogs.asp.net/ricardoperes/archive/2009/09/17/nhibernate-image-user-type.aspx http://weblogs.asp.net/ricardoperes/archive/2009/09/17/nhibernate-image-user-type.aspx
edit. 编辑。 This user type looks a lot better: http://www.martinwilley.com/net/code/nhibernate/usertype.html 这种用户类型看起来好多了: http : //www.martinwilley.com/net/code/nhibernate/usertype.html
I have the following table in PostgreSQL with bytea
column: 我在PostgreSQL中有以下表格,其中包含bytea
列:
CREATE TABLE "SystemFiles"
(
id integer NOT NULL,
name character varying(64),
contenttype character varying(64),
data bytea,
CONSTRAINT pk_id PRIMARY KEY (id)
)
Here is my entity: 这是我的实体:
public class SystemFiles
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string ContentType { get; set; }
public virtual byte[] Data { get; set; }
}
And that's how my mapping looks like: 这就是我的映射的样子:
public class SystemFilesMap : ClassMap<SystemFiles>
{
public SystemFilesMap()
{
Id(x => x.Id, "id").GeneratedBy.Identity();
Map(x => x.Name).Column("name");
Map(x => x.ContentType).Column("contenttype");
Map(x => x.Data).Column("data");
}
}
With above configutration I can read, save, delete files from/into database... 通过上面的配置,我可以从/向数据库中读取,保存,删除文件......
CONTROLLER: 控制器:
using (var session = RisDbHelper.OpenSession())
{
var tempImage = (from c in session.Query<SystemFiles>() where c.Name == "Logo" select c).FirstOrDefault();
model.LogoImage = Convert.ToBase64String(tempImage.Data);
}
VIEW: 视图:
@if (!String.IsNullOrEmpty(Model.LogoImage))
{
<img src="@String.Format("data:image/png;base64,{0}", Model.LogoImage)" style="width: 200px"/>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.