Ejemplo n.º 1
0
        private DataManager()
        {
            _sessionFactory = Fluently.Configure()
                              .Database(() =>
            {
                var result = MsSqlConfiguration.MsSql2012.ConnectionString(c => c.FromConnectionStringWithKey("YokogawaData"));

                if (Config.Current.ShowSql)
                {
                    result.ShowSql();
                }

                return(result);
            })
                              .Mappings(m =>
            {
                m.FluentMappings
                .AddFromAssembly(GetType().Assembly)
                .Conventions.Add(
                    ForeignKey.EndsWith("ID"),
                    LazyLoad.Always()
                    );
            })
                              .CurrentSessionContext <ThreadStaticSessionContext>()
                              .BuildSessionFactory();
        }
Ejemplo n.º 2
0
 public MyAutoPersistenceModel()
 {
     this.AutoPersistenceModel = AutoMap.AssemblyOf <Entity>(new CustomAutomappingConfiguration())
                                 .IgnoreBase <Entity>()
                                 .Override <User>(map => map.IgnoreProperty(x => x.DisplayedName))
                                 .Override <Appointment>(map => map.IgnoreProperty(x => x.DateRange))
                                 .Override <IllnessPeriod>(map => map.IgnoreProperty(p => p.Duration))
                                 .Override <Role>(map => map.HasManyToMany(x => x.Tasks).Cascade.All())
                                 .Override <DbSetting>(map => map.Map(p => p.Key).Unique())
                                 .Override <Patient>(map =>
     {
         map.DynamicUpdate();
         map.IgnoreProperty(x => x.Age);
         map.Map(x => x.IsDeactivated).Default("0").Not.Nullable();
         map.HasMany <Bmi>(x => x.BmiHistory).KeyColumn("Patient_Id");
         map.HasMany <MedicalRecord>(x => x.MedicalRecords).KeyColumn("Patient_Id");
         map.HasMany <IllnessPeriod>(x => x.IllnessHistory).KeyColumn("Patient_Id");
         map.HasMany <Appointment>(x => x.Appointments).KeyColumn("Patient_Id");
     })
                                 .Override <Person>(map =>
     {
         map.Map(p => p.FirstName).Index("idx_person_FirstName");
         map.Map(p => p.LastName).Index("idx_person_LastName");
     })
                                 .Override <ApplicationStatistics>(map =>
     {
         map.Map(e => e.IsExported).Default("0").Not.Nullable();
         map.Map(e => e.Version).Default("\"3.0.3\"").Not.Nullable();
     })
                                 .Conventions.Add(DefaultCascade.SaveUpdate()
                                                  , DynamicUpdate.AlwaysTrue()
                                                  , DynamicInsert.AlwaysTrue()
                                                  , LazyLoad.Always());
 }
        public static IServiceCollection ConfigureDataAccess(this IServiceCollection services, string connectionString)
        {
            var nhConfig = Fluently.Configure()
                           .Database(
                MsSqlConfiguration.MsSql2012.ConnectionString(connectionString)
                .DefaultSchema("dbo")
                .AdoNetBatchSize(20)
                .ShowSql()
                .FormatSql()
                .UseReflectionOptimizer()
                )
                           .Mappings(m =>
                                     m.FluentMappings
                                     .Conventions.Add(
                                         LazyLoad.Always(),
                                         DynamicUpdate.AlwaysTrue())
                                     .Conventions.Add <AssociationsMappingConvention>()
                                     .Conventions.Add <NotNullGuidTypeConvention>()
                                     .AddFromAssembly(Assembly.GetExecutingAssembly())

                                     );

            services.AddSingleton(nhConfig.BuildSessionFactory());
            services.AddScoped(sp =>
            {
                var sessionFactory = sp.GetRequiredService <ISessionFactory>();
                return(sessionFactory.OpenSession());
            });

            return(services);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Fluent NHibernate 에서 제공하는 Convention 설정에 따른 <see cref="IConvention"/> 인스턴스를 빌드하여 제공합니다.
        /// </summary>
        /// <param name="options">ConventionOptions 인스턴스</param>
        /// <returns>Convention 설정 정보를 기초로 만든 <see cref="IConvention"/>의 인스턴스 배열</returns>
        public static IList <IConvention> ToConventions(ConventionOptions options)
        {
            options.ShouldNotBeNull("optioons");

            if (IsDebugEnabled)
            {
                log.Debug("ConventionOptions 정보로 IConvention[]을 빌드합니다...  " + options);
            }

            var conventions = new List <IConvention>();

            conventions.Add((options.DefaultLazy) ? LazyLoad.Always() : LazyLoad.Never());

            if (options.DynamicInsert)
            {
                conventions.Add(FluentNHibernate.Conventions.Helpers.DynamicInsert.AlwaysTrue());
            }
            if (options.DynamicUpdate)
            {
                conventions.Add(FluentNHibernate.Conventions.Helpers.DynamicUpdate.AlwaysTrue());
            }

            if (options.TableNamePrefix.IsNotWhiteSpace())
            {
                conventions.Add(Table.Is(x => options.TableNamePrefix + x.EntityType.Name));
            }
            else if (options.TableNamePrefix.IsNotWhiteSpace())
            {
                conventions.Add(Table.Is(x => x.EntityType.Name + options.TableNameSurfix));
            }
            else
            {
                conventions.Add(Table.Is(x => x.EntityType.Name));
            }

            if (options.PrimaryKeyName.IsNotWhiteSpace())
            {
                conventions.Add(PrimaryKey.Name.Is(x => options.PrimaryKeyName));
            }
            else if (options.PrimaryKeySurfix.IsNotWhiteSpace())
            {
                conventions.Add(PrimaryKey.Name.Is(x => x.EntityType.Name + options.PrimaryKeySurfix));
            }

            if (options.ForeignKeySurfix.IsNotWhiteSpace())
            {
                conventions.Add(ForeignKey.EndsWith(options.ForeignKeySurfix));

                conventions.Add(ConventionBuilder.HasMany.Always(x => x.Key.Column(x.EntityType.Name + options.ForeignKeySurfix)));
                conventions.Add(ConventionBuilder.Reference.Always(x => x.Column(x.Property.Name + options.ForeignKeySurfix)));

                conventions.Add(ConventionBuilder.HasManyToMany.Always(x => {
                    x.Key.Column(x.EntityType.Name + options.PrimaryKeySurfix);
                    x.Relationship.Column(x.ChildType.Name +
                                          options.ForeignKeySurfix);
                }));
            }

            return(conventions);
        }
Ejemplo n.º 5
0
 private static Action <FluentNHibernate.Conventions.IConventionFinder> GetConventions()
 {
     return(mappings =>
     {
         mappings.Add <PrimaryKeyConvention>();
         mappings.Add <TableNameConvention>();
         mappings.Add <EnumConvention>();
         mappings.Add(ForeignKey.EndsWith("Id"));
         mappings.Add(DefaultCascade.None());
         mappings.Add(DefaultAccess.Property());
         mappings.Add(DefaultLazy.Always());
         mappings.Add(LazyLoad.Always());
     });
 }
Ejemplo n.º 6
0
        private void HandleMappings(MappingConfiguration cfg)
        {
            cfg.HbmMappings.AddFromAssemblyOf <SessionManager <T> >();

            cfg.FluentMappings
            .AddFromAssembly(GetType().Assembly)
            .Conventions.AddAssembly(GetType().Assembly)
            .Conventions.Add(
                AutoImport.Always(),
                ForeignKey.EndsWith("ID"),
                LazyLoad.Always(),
                DefaultCascade.None(),
                DynamicInsert.AlwaysTrue(),
                DynamicUpdate.AlwaysTrue());
        }
Ejemplo n.º 7
0
 private static FluentConfiguration GetFluentConfigurationForDatabase(string connectionString)
 {
     return(Fluently.Configure()
            .Database(
                MsSqlConfiguration.MsSql2012
                .Provider <HybridStorageDriverConnectionProvider>()
                .ConnectionString(connectionString)
                .DefaultSchema("dbo")
                .AdoNetBatchSize(20)
                .ShowSql()
                .FormatSql()
                .UseReflectionOptimizer()
                )
            .Mappings(m =>
                      m.FluentMappings
                      .Conventions.Add(
                          LazyLoad.Always(),
                          DynamicUpdate.AlwaysTrue())
                      .Conventions.Add <AssociationsMappingConvention>()
                      .Conventions.Add <NotNullGuidTypeConvention>()
                      .AddFromAssembly(Assembly.GetExecutingAssembly())));
 }