Mapping for a nullable property results in 0 but not Null

I have the following mapping defined using FluentMapping

public class RuleMap : ClassMap<Rule>
{
    public RuleMap()
    {
        Table("NEW");

        Id(x => x.Id, "Id").Not.Nullable();
        Map(x => x.SenderId, "SenderId").Nullable();
    }
}

for the next class

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

    public virtual int? SenderId { get; set; }
}

My table in the database is defined as

USE [Test]
GO

/****** Object:  Table [dbo].[NEW]    Script Date: 23.04.2015 22:14:53 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[NEW](
    [Id] [int] NOT NULL,
    [SenderId] [int] NULL,
 CONSTRAINT [PK_NEW] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

and has one record with

Id  | SenderId
 1  |  `NULL`

Now when I load all Rules through my SessionFactory

var rules = sessionFactory.QueryOvery<Rule>.List();

the list contains 1 element (how good it is), but the property SenderIdis 0 and not NULL , as I expected.

So what am I doing wrong here?

Using NHibernate 3.1 and FluentNHibernate 1.2.

+4
source share
1 answer

Change your mapping,

public class NullableMap : ClassMap<Nullable>
{
    public NullableMap()
    {
        Table("nullable");
        LazyLoad();
        Id(x => x.Id).GeneratedBy.Identity().Column("id");
        Map(x => x.SenderId, "SenderId").Nullable().Default(null);
    }
}

null (.Default(null);)

+2

Source: https://habr.com/ru/post/1584358/


All Articles