System.Drawing.Image 的 Nhibernate 映射

发布于 2024-09-26 02:40:38 字数 712 浏览 2 评论 0原文

问题:将此类序列化为 nHibernate xml 文件时出现异常({“无法确定类型:System.Drawing.Image、System.Drawing、列:NHibernate.Mapping.Column(Settings)”})。

如何将 System.Drawing.Image 映射到 nHibernate ? 以及要使用什么 MS-SQL 数据库类型?

using System;
using System.Collections.Generic;
using System.Text;

namespace nhDBapi.Tables
{

    [NHibernate.Mapping.Attributes.Class(Name = "nhDBapi.Tables.clsSettings, nhDBapi", Table = "lsSettings")]
    public class clsSettings
    {

        [NHibernate.Mapping.Attributes.Id(Name = "Settings", Column = "Settings", TypeType = typeof(System.Drawing.Image))]
        public System.Drawing.Image Settings;

    } // End partial class lsSettings


} // End Namespace nhDBapi.Tables

Question: I get an exception serializing this class to a nHibernate xml file ({"Could not determine type for: System.Drawing.Image, System.Drawing, for columns: NHibernate.Mapping.Column(Settings)"}).

How to map System.Drawing.Image to nHibernate ?
And what MS-SQL dbtype is to be used?

using System;
using System.Collections.Generic;
using System.Text;

namespace nhDBapi.Tables
{

    [NHibernate.Mapping.Attributes.Class(Name = "nhDBapi.Tables.clsSettings, nhDBapi", Table = "lsSettings")]
    public class clsSettings
    {

        [NHibernate.Mapping.Attributes.Id(Name = "Settings", Column = "Settings", TypeType = typeof(System.Drawing.Image))]
        public System.Drawing.Image Settings;

    } // End partial class lsSettings


} // End Namespace nhDBapi.Tables

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

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

发布评论

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

评论(3

紙鸢 2024-10-03 02:40:38

我不建议直接映射到 System.Drawing.Image。它不仅是一次性的(NHibernate 必须处理它,我不确定它可以),而且如果您获取 clsSettings 集合,您将创建大量 Image 实例,从而浪费CPU 和内存(如果您不全部使用的话)。

相反,映射到带有 sql 类型 varbinary 的 byte[],并且您根据需要处理从 Image 到 Image 的转换。 示例

另外值得一看的是这个关于 NHibernate 的大对象存储支持,似乎比映射到 byte[] 更有效,而且它也是一篇关于所有内容的优秀文章选项。

I wouldn't recommend mapping directly to System.Drawing.Image. Not only is it disposable (NHibernate would have to dispose it and I'm not sure it can), but also if you fetch a collection of clsSettings you'll be creating lots of Image instances, thus wasting CPU and memory if you don't use all of them.

Instead, map to a byte[] with sql type varbinary and you handle converting from and to Image as necessary. Example.

Also worth checking out is this project about large object storage support for NHibernate, seems to be more efficient than mapping to a byte[] and it's also an excellent article about all options.

何以笙箫默 2024-10-03 02:40:38

您必须使用 IUserType。
查看此示例(包括映射)

You have to use an IUserType.
See this example including mappings

酒解孤独 2024-10-03 02:40:38

您提倡将图像存储在数据库中,但这并不是一个好主意,因为开销巨大。你这样做有充分的理由吗?这是网络应用程序还是客户端桌面应用程序?

更好的解决方案是将图像存储在文件系统中,并使用数据库存储有关图像的元数据,例如文件名、标题等。

但正如 Mauricio Scheffer 所说,如果您必须这样做,那么您可能会更好使用 byte[] 数组,或者使用一些封装 byte[] 数组的自定义类型并为您提供辅助方法,例如 public Image GetBitmapImage();

You're advocating storing an image in the database, this is rarely a good idea because of the huge overhead. Do you have a good reason for doing this? Is this a web app or a client desktop app?

Far better solution is to store the image in the file system and use the database to store meta-data about the image, such as filename, caption etc.

But as Mauricio Scheffer says, if you must do this then you're probably better off using a byte[] array, or maybe use some custom type that encapsulates a byte[] array and gives you helper methods such as public Image GetBitmapImage();

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