2013-11-15 3 views
0

내가NHibernate에 오류 : 바이트 [] 값의 길이가

I`ve 발견 자 NHibernate는 MSSQL 2008 준수와 함께 사용하여 파일을 저장할 매핑/매개 변수에 구성된 길이를 초과 기사를 수행하는 방법 및 모든 것을 구현하십시오. http://harmonypsa.blogspot.com/2013/07/using-filestream-with-sql-server-and.html

내가 코드를 실행할 때 "byte [] 값의 길이가 매핑/매개 변수에 구성된 길이를 초과합니다." 여기 내 클래스 :

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

내 매핑 :

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

어떤 아이디어가?

답변

1

이 코드를 업데이트했습니다 : public class TestEntity : BaseEntity { public virtual Guid ImageId {get; 세트; }

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

그리고 매퍼에 문자열 추가 : Document.hbm.xml 파일에서

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

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

테이블의 이미지 열입니다 MySQL 서버 2008 Express 데이터베이스에서 varbinary (MAX)를 입력하고 MsSql2008 방언을 사용하고 있습니다.

따라 가기 - https://nhibernate.jira.com/browse/NH-2484