Esempio n. 1
0
        public override IServiceProvider CreateServiceProvider(IServiceCollection services)
        {
            return(services.AddAutofacProvider(builder =>
            {
                //nhibernate
                var nhibernateModule = new LoquaciousNHibernateModule <MsSql2012Dialect, Sql2008ClientDriver>
                {
                    SchemaRootPath = Environment.ContentRootPath,
                    OnModelCreating = (cfg, mappings, types) =>
                    {
                        var conventionBuilder = new ConventionBuilder
                        {
#if DEBUG
                            OutputXmlMappingsFile = Path.Combine(Environment.ContentRootPath, "schema.hbm.xml"),
                            ShowLogs = true,
#endif
                            AutoMappingOverride = (modelMapper) =>
                            {
                                modelMapper.BeforeMapClass += (modelInspector, type, map) =>
                                {
                                    map.Id(k =>
                                    {
                                        k.Generator(Generators.Native);
                                    });
                                };
                                modelMapper.Class <Culture>(m => m.Id(q => q.Name, o => o.Generator(Generators.Assigned)));
                                modelMapper.Class <LocalizedStringResource>(m => m.ComposedId(q =>
                                {
                                    q.Property(x => x.Resource);
                                    q.Property(x => x.Key);
                                }));
                                modelMapper.AddMappings(mappings);
                            },
                            Conventions = new List <IAmConvention> {
                                new BidirectionalManyToManyRelationsConvention(),
                                new BidirectionalOneToManyConvention(),
                                new EnumConvention(),
                                new UnidirectionalManyToOne(),
                                new UnidirectionalOneToManyConvention(),
                                new NamingConvention()
                            }
                        };
                        conventionBuilder.ProcessConfiguration(cfg, types.Where(q => q.IsEnum == false));
                    },
                }.AddAssemblyFor <User>();
                nhibernateModule.OnSessionFactoryCreated += Seed.PopulateDatabase;
                builder.RegisterModule(nhibernateModule);

                //services
                var servicesModule = new ServiceModule().AddAssemblyFor <Startup>();
                builder.RegisterModule(servicesModule);
            }));
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            TestConnectionProvider.CloseDatabase();
            var cfg = NHConfigurator.Configuration;
            var conventionBuilder = new ConventionBuilder
            {
                DbSchemaOutputFile       = "schema.sql",
                DropTablesCreateDbSchema = true,
                OutputXmlMappingsFile    = "schema.hbm.xml",
                MappingsAssembly         = typeof(Entity).Assembly,
                ShowLogs       = true,
                FilterAssembly = t => t.IsSubclassOf(typeof(Entity)),
                Conventions    = new List <IAmConvention> {
                    new VersionConvention(),
                    new BidirectionalManyToManyRelationsConvention
                    {
                        BaseEntityType = typeof(Entity),
                    },
                    new BidirectionalOneToManyConvention
                    {
                        BaseEntityType = typeof(Entity),
                    },
                    new DefineBaseClassConvention
                    {
                        BaseEntityToIgnore = typeof(Entity)
                    },
                    new EnumConvention(),
                    new NamingConvention(),
                    new UnidirectionalManyToOne
                    {
                        BaseEntityType = typeof(Entity),
                    },
                    new UnidirectionalOneToManyConvention
                    {
                        BaseEntityType = typeof(Entity),
                    },
                    new ReadOnlyConvention
                    {
                        BaseEntityType = typeof(Entity),
                    }
                }
            };

            conventionBuilder.ProcessConfiguration(cfg);
            var session = SessionFactory.OpenSession();

            CurrentSessionContext.Bind(session);
            using (var tx = session.BeginTransaction())
            {
                //create a person
                Person p = new Person("Bruce", "Wayne");
                p.DateOfBirth   = new DateTime(1972, 6, 2);
                p.HomePhone     = "12345678";
                p.AltPhone      = "987675123";
                p.Address.Line1 = "123 Bruce Manor";
                p.Address.State = "GT";
                p.Address.City  = "Gotham";
                p.Address.Zip   = "99999";
                //save the person
                session.Save(p);
                Assert.IsTrue(p.Id != 0, "Save should give p it’s primary key");
                Person pVerify = (Person)session.Load(typeof(Person), p.Id);
                Assert.AreEqual(p, pVerify, "They weren’t the same");
            }
        }