Search code examples
sql-server-2008nhibernateconform

Nhibernate error: The length of the byte[] value exceeds the length configured in the mapping/parameter


I want to save files using Nhibernate with Conform to MSSQL 2008.

I`ve found article how to do it and implement everything. http://harmonypsa.blogspot.com/2013/07/using-filestream-with-sql-server-and.html

When I run my code I get the exception: "The length of the byte[] value exceeds the length configured in the mapping/parameter." Here is my class:

public class BaseEntity
    {
        public virtual int Id { get; set; } 
    }
public class TestEntity : BaseEntity
    {
        public virtual Guid ImageId { get; set; }

        public virtual Binary Image { get; set; }
    }

My mapping:

   class Program
    {
        static void Main(string[] args)
        {
            var factory = BuildFactory();

            using (ISession session = factory.OpenSession())
            {
                using (ITransaction tx = session.BeginTransaction())
                {
                    using (FileStream fi = File.OpenRead(@"C:\A\2013-10-31_1633.png"))
                    {
                        var photoBytes = new byte[fi.Length];
                        fi.Read(photoBytes, 0, photoBytes.Length);

                        var te = new TestEntity { Image = photoBytes };
                        session.SaveOrUpdate(te);
                        tx.Commit();
                    }
                }
            }
        }

        internal static Configuration GetConfiguration()
        {
            DomainMapper mapper = new DomainMapper();
            HbmMapping generatedMappigs = mapper.GenerateMappigs();

            var cfg = new Configuration();
            cfg.SessionFactory().Proxy.Through<NHibernate.Bytecode.DefaultProxyFactoryFactory>().Integrate.Using<MsSql2008Dialect>()
                .AutoQuoteKeywords().Connected.By<SqlClientDriver>().ByAppConfing("DBConnectionString").CreateCommands
                .ConvertingExceptionsThrough<SQLStateConverter>();
            cfg.SetProperty("show_sql", "true");
            cfg.SetProperty("format_sql", "true");
            cfg.AddDeserializedMapping(generatedMappigs, string.Empty);

            new SchemaUpdate(cfg).Execute(true, true);
            return cfg;
        }

        private static ISessionFactory BuildFactory()
        {
            Configuration cfg = GetConfiguration();
            return cfg.BuildSessionFactory();
        }

public HbmMapping GenerateMappigs()
        {
            IEnumerable<Type> domainEntities = this.GetDomainEntities();

            ObjectRelationalMapper relationalMapper = new ObjectRelationalMapper();
            relationalMapper.TablePerConcreteClass(domainEntities); 

            Mapper mapper = new Mapper(relationalMapper);
            HbmMapping mapping = mapper.CompileMappingFor(domainEntities); 
            //File.WriteAllText(@"d:\mappings.xml", Serialize(mapping)); 
            return mapping;
        }

        /// <summary>
        /// Gets all objects that are inherited from EntityBase.
        /// </summary>
        private IEnumerable<Type> GetDomainEntities()
        {
            Assembly domainAssembly = typeof(BaseEntity).Assembly;
            IEnumerable<Type> domainEntities = from t in domainAssembly.GetTypes()
                                               where t.BaseType == typeof(BaseEntity) && !t.IsGenericType
                                               select t;
            return domainEntities;
        }
    }

Any ideas?


Solution

  • I have updated this code: public class TestEntity : BaseEntity { public virtual Guid ImageId { get; set; }

        public virtual System.Byte[] Image { get; set; }
    }
    

    And added this string to the mapper:

    mapper.Class<TestEntity>(map => map.Property(o => o.Image, pm => pm.Type(NHibernateUtil.BinaryBlob)));