What kind of Type needs to store images in database

Topics: Customizing Orchard, General
Dec 26, 2012 at 6:50 PM

Hi all,

What kind of Type needs to store images in database? Is it varbinary? How can i set up it in the migration class?

 

Thanks.

Dec 29, 2012 at 4:34 PM
Edited Dec 29, 2012 at 4:39 PM

Ok, I wrote this code in the migration class:

SchemaBuilder.CreateTable("BigAttachRecord",
    table => table

        //---< General Properties
        .Column<int>("Id", column => column.PrimaryKey().Identity())
        .Column<int>("General_Id", column => column.Unique())

        .Column<string>("Ext01", column => column.WithDefault(""))
        .Column<string>("FileName01", column => column.WithDefault(""))
        .Column("file01", DbType.Binary, column => column.Unlimited())
        );

and this for "BigAttachRecord" class:

    public class BigAttachRecord
    {
        public virtual int Id { get; set; }
        public virtual int General_Id { get; set; }

        public virtual string Ext01 {get; set;}
        public virtual string FileName01 {get; set;}
        public virtual Byte[] file01 { get; set; }
    }


I want to store an image in database. First i convert it to an array of bytes then try to write them in this way:

 

        public BigAttachRecord AddFile(BigAttachRecord file)
        {
            if (file == null)
                return null;
            _repositoryAttach.Create(file);
        }

The problem is appeared when i try to store an image with size of 300 kb or more.
When the type of database is Sql Compact, it works well, but when the type of database is Sql server, it doesn't work.

Has anybody have any ideas?

Thanks for help.

The last lines of stack is like this:

yClass13.<InvokeActionMethodWithFilters>b__10()
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
   at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
2012-12-29 18:16:42,734 [25] Orchard.Exceptions.DefaultExceptionPolicy - An unexpected exception was caught
NHibernate.PropertyValueException: Error dehydrating property value for Dophlin.RegOrder.Models.RegCustomerBigAttachRecord.file01 ---> NHibernate.HibernateException: The length of the byte[] value exceeds the length configured in the mapping/parameter.
   at NHibernate.Type.AbstractBinaryType.Set(IDbCommand cmd, Object value, Int32 index)
   at NHibernate.Type.NullableType.NullSafeSet(IDbCommand cmd, Object value, Int32 index)
   at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index)
   --- End of inner exception stack trace ---
   at NHibernate.Persister.Entity.AbstractEntityPersister.Dehydrate(Object id, Object[] fields, Object rowId, Boolean[] includeProperty, Boolean[][] includeColumns, Int32 table, IDbCommand statement, ISessionImplementor session, Int32 index)
   at NHibernate.Persister.Entity.AbstractEntityPersister.GeneratedIdentifierBinder.BindValues(IDbCommand ps)
   at NHibernate.Id.Insert.AbstractReturningDelegate.PerformInsert(SqlCommandInfo insertSQL, ISessionImplementor session, IBinder binder)
   at NHibernate.Persister.Entity.AbstractEntityPersister.Insert(Object[] fields, Object obj, ISessionImplementor session)
   at NHibernate.Action.EntityIdentityInsertAction.Execute()
   at NHibernate.Engine.ActionQueue.Execute(IExecutable executable)
   at NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object entity, EntityKey key, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
   at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
   at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
   at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
   at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
   at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
   at NHibernate.Impl.SessionImpl.Save(Object obj)
   at Dophlin.RegOrder.Services.RegCustomerService.AddFile(BigAttachRecord file)
   at Dophlin.RegOrde

Developer
Dec 31, 2012 at 8:03 PM
NHibernate.PropertyValueException: Error dehydrating property value for Dophlin.RegOrder.Models.RegCustomerBigAttachRecord.file01 ---> NHibernate.HibernateException: The length of the byte[] value exceeds the length configured in the mapping/parameter

Looks like a bug, since you did specify Unlimited as the data type's length.

Feb 27, 2013 at 3:45 PM
You also have to put
[StringLength(1000000)]
public virtual Byte[] file01 { get; set; }

SchemaBuilder.CreateTable is only for creating table, so "DbType.Binary, column => column.Unlimited()" means that column "file01" will have type varbinary(MAX).

[StringLength(1000000)] in record class is for mapping.