0

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?

4

2 回答 2

3

在 Document.hbm.xml 文件中,

<property name="Image" type="System.Drawing.Image, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<column name="Image" length="2147483647" not-null="false" />
</property>

表中的 Image 列是 MySQL Server 2008 Express 数据库中的 varbinary(MAX) 类型,我使用的是 MsSql2008 方言。

按照这个链接- https://nhibernate.jira.com/browse/NH-2484

于 2015-05-16T12:45:55.737 回答
1

我已经更新了这段代码: public class TestEntity : BaseEntity { public virtual Guid ImageId { get; 放; }

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

并将此字符串添加到映射器:

mapper.Class<TestEntity>(map => map.Property(o => o.Image, pm => pm.Type(NHibernateUtil.BinaryBlob)));
于 2013-11-15T20:51:22.193 回答