Sunday, May 27, 2018

NHibernate manual mapping-by-code

namespace TestNH
{
    using System;
    using NHibernate.Cfg;

    class Program
    {
        static async System.Threading.Tasks.Task Main(string[] args)
        {
            var cfg = new NHibernate.Cfg.Configuration();

            cfg.DataBaseIntegration(c =>
            {
                c.Driver<NHibernate.Driver.NpgsqlDriver>();
                c.Dialect<NHibernate.Dialect.PostgreSQLDialect>();

                c.ConnectionString = "Server=localhost; Port=5432; Database=test; User Id=postgres; Password=opensesame93";   

                c.LogFormattedSql = true;
                c.LogSqlInConsole = true;

            });


            var mapper = new NHibernate.Mapping.ByCode.ModelMapper();
            mapper.AddMapping<TheTimeMapping>();
            
            var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();

            cfg.AddMapping(mapping);

            var sf = cfg.BuildSessionFactory();


            using (var session = sf.OpenSession())
            using (var tx = session.BeginTransaction())
            {
                var utcString = "2018-05-04T17:37:00.0000000Z";

                var local = DateTime.Parse(utcString);
                var utc = DateTime.Parse(utcString).ToUniversalTime();

                Console.WriteLine($"local value is {local} kind is {local.Kind}");
                Console.WriteLine($"utc value is {utc} kind is {utc.Kind}");                

                var t = new Test
                {
                    A = local,
                    B = utc,
                    C = local,
                    D = utc
                };
                                              
                await session.PersistAsync(t);

                await tx.CommitAsync();
            }                       
        }
    }


    public class Test
    {
        public virtual int Id { get; set; }

        public virtual DateTime A { get; set; }
        public virtual DateTime B { get; set; }
        public virtual DateTime C { get; set; }
        public virtual DateTime D { get; set; }
    }

    public class TheTimeMapping: NHibernate.Mapping.ByCode.Conformist.ClassMapping<Test>
    {
        public TheTimeMapping()
        {
            Id(x => x.Id, id =>
            {                
                id.Generator(
                    NHibernate.Mapping.ByCode.Generators.Sequence, 
                    generatorMapping => generatorMapping.Params(new { sequence = "test_id_seq"})
                );               
            });

            Property(p => p.A);
            Property(p => p.B);
            Property(p => p.C);
            Property(p => p.D);            
        }
    }

    /*
        create table test
        (
            id int generated by default as identity primary key,
            a timestamp not null,
            b timestamp not null,
            c timestamptz not null,
            d timestamptz not null
        );
     */
}



Outputs:
local value is 05/05/2018 01:37:00 kind is Local
utc value is 05/04/2018 17:37:00 kind is Utc


Database:



No comments:

Post a Comment