nhibernate.postgresql.json 1.0.0

NHibernate CustomType to persist/load to JSON/JSONB column types in PostgreSql.

Works with NHibernate 5.x. and .NET Core 2.1

There is a newer version of this package available.
See the version list below for details.
Install-Package nhibernate.postgresql.json -Version 1.0.0
dotnet add package nhibernate.postgresql.json --version 1.0.0
<PackageReference Include="nhibernate.postgresql.json" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add nhibernate.postgresql.json --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

nhibernate.postgresql.json

NHibernate custom type to handle PostgreSQL JSON and JSONB column types

Installation

This library depends on the following NuGet packages:

These will be installed as dependencies along with this page.

To install this download package nhibernate.postgresql.json or run:

Install-Package nhibernate.postgresql.json

Compatibility

This library has been tested with .NET Core 2.1 and NHibernate 5.x.

Configuration

All this package does is offer an NHibernate custom type that you can use to handle json/jsonb PostgreSql data types. It relies on the configuration provided by NpgSql.Json.Net.

The way it works is by telling the extension which .NET types you want it to handle for you. This is done in the Startup class:

NpgsqlConnection.GlobalTypeMapper.UseJsonNet(new[] { typeof(AnotherType) });

It is important to note that you must specify each of the types that you want the library to handle for you and not their parents.

Usage

After the configuration is complete, you can just use it in your mapping as a custom type:

Map(x => x.PropertyWithMyType).CustomSqlType("jsonb").CustomType<JsonType<AnotherType>>().Column("db_column");

Sample

A more complete sample can be found below:

Startup.cs

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            NpgsqlConnection.GlobalTypeMapper.UseJsonNet(new[] { typeof(MyType) });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            //app.UseHttpsRedirection();
            app.UseMvc();
        }

MyEntity.cs

public class AnotherType
{
	public AnotherType() {}
	
	public virtual string AnotherProperty {get;set;}
}

public class MyType
{
	public MyType() {}

	public virtual string Name { get; set; }
	public virtual int Age { get; set; }
	public virtual AnotherType Other { get; set; }	
}

public class MyTypeMap : ClassMap<AnotherType>
{

	public MyType()
	{
		Table("my_table");

		Map(x => x.Name).Column("name");
		Map(x => x.Age).Column("age");
		Map(x => x.Other).CustomSqlType("jsonb").CustomType<JsonType<Transaction>>().Column("other").Not.Nullable();
	}
}

nhibernate.postgresql.json

NHibernate custom type to handle PostgreSQL JSON and JSONB column types

Installation

This library depends on the following NuGet packages:

These will be installed as dependencies along with this page.

To install this download package nhibernate.postgresql.json or run:

Install-Package nhibernate.postgresql.json

Compatibility

This library has been tested with .NET Core 2.1 and NHibernate 5.x.

Configuration

All this package does is offer an NHibernate custom type that you can use to handle json/jsonb PostgreSql data types. It relies on the configuration provided by NpgSql.Json.Net.

The way it works is by telling the extension which .NET types you want it to handle for you. This is done in the Startup class:

NpgsqlConnection.GlobalTypeMapper.UseJsonNet(new[] { typeof(AnotherType) });

It is important to note that you must specify each of the types that you want the library to handle for you and not their parents.

Usage

After the configuration is complete, you can just use it in your mapping as a custom type:

Map(x => x.PropertyWithMyType).CustomSqlType("jsonb").CustomType<JsonType<AnotherType>>().Column("db_column");

Sample

A more complete sample can be found below:

Startup.cs

public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc()
                .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            NpgsqlConnection.GlobalTypeMapper.UseJsonNet(new[] { typeof(MyType) });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            //app.UseHttpsRedirection();
            app.UseMvc();
        }

MyEntity.cs

public class AnotherType
{
	public AnotherType() {}
	
	public virtual string AnotherProperty {get;set;}
}

public class MyType
{
	public MyType() {}

	public virtual string Name { get; set; }
	public virtual int Age { get; set; }
	public virtual AnotherType Other { get; set; }	
}

public class MyTypeMap : ClassMap<AnotherType>
{

	public MyType()
	{
		Table("my_table");

		Map(x => x.Name).Column("name");
		Map(x => x.Age).Column("age");
		Map(x => x.Other).CustomSqlType("jsonb").CustomType<JsonType<Transaction>>().Column("other").Not.Nullable();
	}
}

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.1.2 68 7/26/2019
1.1.1 88 5/27/2019
1.1.0 107 5/17/2019
1.0.21 73 5/27/2019
1.0.1 148 12/26/2018
1.0.0 140 12/26/2018
Show less