Aliendreamer

nhibernate problem with sqlite

May 27th, 2019
514
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. using System.Reflection;
  2. using FluentNHibernate.Cfg;
  3. using FluentNHibernate.Cfg.Db;
  4. using NHibernate;
  5. using NHibernate.Cfg;
  6. using NHibernate.Tool.hbm2ddl;
  7.  
  8. namespace Oss.Core.Helpers.Test
  9. {
  10.     public class InMemorySessionProvider
  11.     {
  12.         private static InMemorySessionProvider _instance;
  13.  
  14.         public static InMemorySessionProvider Instance => _instance ?? (_instance = new InMemorySessionProvider());
  15.  
  16.         private static ISessionFactory _sessionFactory;
  17.         private static Configuration _configuration;
  18.         private static SchemaExport _schemaExport;
  19.         private static ISession _session;
  20.         private InMemorySessionProvider() { }
  21.  
  22.         public void Initialize()
  23.         {
  24.             CreateSession();
  25.         }
  26.         private static void CreateSession()
  27.         {
  28.             //взимаме всички мапинг намират се в domain по дизайн
  29.             var domain = Assembly.Load("Domain");
  30.             var config = Fluently.Configure()
  31.                 .Database(SQLiteConfiguration.Standard.InMemory()
  32.                     .ConnectionString("Data Source=:memory:;Version=3;New=True")
  33.                     .ShowSql());
  34.             config.Mappings(x => x.FluentMappings.AddFromAssembly(domain));
  35.             config.ExposeConfiguration(cfg => { _configuration = cfg; }).BuildConfiguration();
  36.             //създаваме fluent конфигурацията.  в foreach сменяме dbo с "" иначе sqllite хвърля exception
  37.             // оказва се че ако'dbName'dbo.'dbtable' тои си мисли че dbo e нова база
  38.             foreach (var pc in _configuration.ClassMappings)
  39.             {
  40.            
  41.                 if (!pc.Table.Schema.Contains("dbo")) continue;
  42.                 var newName = pc.Table.Schema.Replace(".dbo", "");
  43.                 pc.Table.Schema = newName;
  44.             }
  45.             var a = _configuration.ClassMappings;
  46.  
  47.  
  48.             _sessionFactory = _configuration.BuildSessionFactory();
  49.             _schemaExport = new SchemaExport(_configuration);
  50.         }
  51.  
  52.         public ISession OpenSession()
  53.         {
  54.             //създаваме новата сесия с заредената schema            
  55.             //и тук идва бъг-а които е труден за възпроизвеждане
  56.             // Message: System.Data.SQLite.SQLiteException : SQL logic error
  57.             //near "max": syntax error
  58.             // теоритично трябва да имаш този проблем ако ползваш guid long bigInt
  59.             //но не се възпроизвежда в отделните микросървиси в  10 микросървиса работи
  60.             // в 2 хвърля грешка....
  61.             //като интересното е че дори да смениш Long guid в 2та проблемни сървиса пак си хвърля грешката.
  62.             _session = _sessionFactory.OpenSession();
  63.             _schemaExport.Execute(false,
  64.                 true, false, _session.Connection,
  65.                 null);
  66.             return _session;
  67.         }
  68.  
  69.         public void Dispose()
  70.         {
  71.             _session.Close();
  72.             _sessionFactory?.Dispose();
  73.             _configuration = null;
  74.             _schemaExport = null;
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment