FluentNHibernate映射设置-SQL Server数据库中的文件存储

发布于 2024-09-13 10:04:54 字数 346 浏览 2 评论 0 原文

我读了很多东西,但没有看到具体的东西,所以我重新发布。抱歉,如果我错过了一篇并重复发布。

我将文件存储在数据库中;以前,在 ADO.NET 实体框架中,我会使用图像类型并将其作为 byte[] 数组进行流式传输。

这是在 NHibernate 中使用 FluentNHibernate 映射执行此操作的方法吗?我将列设置为图像。我将其定义为属性的映射(C# 属性是一个 byte[] 数组):

Map(i => i.FileContents).CustomSqlType("image");

这是设置它的正确方法吗?我收到错误,不确定是否与此有关?

谢谢。

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.

Is that the approach to do it in NHibernate with FluentNHibernate mappings? I setup the column as image. I defined this as the mapping for the property (which the C# property is a byte[] array):

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

与酒说心事 2024-09-20 10:04:54

您还可以将 Custom 映射到 NHibernate.Type 类型

例如:

Map(i => i.FileContents).Custom<NHibernate.Type.BinaryBlobType>();

我已映射存储为二进制的文件,但它们不是“图像”类型。

我进行了快速的谷歌搜索,发现了一篇带有 ImageUserType 的帖子,您可以尝试指定它。
http://weblogs.asp。 net/ricardoperes/archive/2009/09/17/nhibernate-image-user-type.aspx

编辑。此用户类型看起来好多了: http://www.martinwilley.com/网/代码/nhibernate/usertype.html

You can also map Custom<TType>s to NHibernate.Type types

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.
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

梅窗月明清似水 2024-09-20 10:04:54

您不需要自定义类型。这是一个适用于名为 Content 的 SQL Server 图像列的映射:

    Map(x => x.Content);

这是该映射的用法:

byte[] content = nhSession.CreateCriteria<AttachmentContent>()
                .Add<AttachmentContent>(ac => ac.Id == 3)
                .SetProjection(Projections.Property("Content"))
                .UniqueResult<byte[]>();

...这是一种无需映射即可获取它的方法(AttachmentDTO 不是映射的 NH 类,只是一个普通类) :

nhSession.CreateSQLQuery("select a.Content from Attachments a where a.Id = 1")
    .SetResultTransformer(Transformers.AliasToBean(typeof(AttachmentDTO)))
    .UniqueResult<AttachmentDTO>();

这是 DTO 类:

public class AttachmentDTO {
    public int Id { get; set; }
    public string ContentType { get; set; }
    public byte[] Content { get; set; }
}

祝你好运!

You don't need a custom type. Here is a mapping that works for a SQL Server image column named Content:

    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) :

nhSession.CreateSQLQuery("select a.Content from Attachments a where a.Id = 1")
    .SetResultTransformer(Transformers.AliasToBean(typeof(AttachmentDTO)))
    .UniqueResult<AttachmentDTO>();

Here's the DTO class:

public class AttachmentDTO {
    public int Id { get; set; }
    public string ContentType { get; set; }
    public byte[] Content { get; set; }
}

Good luck!

热情消退 2024-09-20 10:04:54

我在 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)
)

我的实体:

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; }
}

这就是我的映射的样子:

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

通过上述配置,我可以从数据库中读取、保存、删除文件...

这是 :

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

看法:

@if (!String.IsNullOrEmpty(Model.LogoImage))
{
    <img src="@String.Format("data:image/png;base64,{0}", Model.LogoImage)" style="width: 200px"/>
}

I have the following table in PostgreSQL with bytea column:

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"/>
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文