FluentNHibernate display settings - storing files in a SQL Server database

I read different things, but I did not see anything specific, so I am rewriting. Sorry if I missed one and duplicated.

I store files in a database; earlier, with the ADO.NET Entity Framework, I would use an image type, and it would pass it as a byte [] array.

Is this an approach to take in NHibernate with FluentNHibernate mapping? I set the column as an image. I defined this as a mapping for a property (which C # property is a byte [] array):

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

Is this the right way to fix this? I am getting an error message and not sure if this is related to this?

Thank.

+3
source share
3 answers

You can also map Custom<TType>to NHibernate.Typetypes

For instance:

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

I matched files stored as binary, but they were not of type "image".

I quickly looked through Google and found a message with ImageUserType that you could try specifying instead. http://weblogs.asp.net/ricardoperes/archive/2009/09/17/nhibernate-image-user-type.aspx

to change. This type of user looks much better: http://www.martinwilley.com/net/code/nhibernate/usertype.html

+1
source

You do not need a special type. Here is the mapping that works for a SQL Server image column named Content:

    Map(x => x.Content);

Here is the use of this mapping:

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

!

+1

I have the following table in PostgreSQL with a column 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 essence:

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 what my comparison 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 the above configuration, I can read, save, delete files from / to the 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"/>
}
0
source

Source: https://habr.com/ru/post/1758474/


All Articles