olidev
olidev

Reputation: 20644

Error dehydrating property value with fluent Nhibernate with bit

I have this class:

public class Person
{
   public virtual long     ID            { get; set; }
   public virtual string   FirstName     { get; set; }
   public virtual string   LastName      { get; set; }
   public virtual boolean  IsValid       { get; set; }
}

and person data mapping:

public class PersonMap : ClassMap<Person>
    {
        public PersonMap()
        {
            Id(x => x.ID);
            Map(x => x.FirstName).Not.Nullable().Length(100);
            Map(x => x.LastName).Not.Nullable().Length(100);
            Map(x => x.IsValid).Not.Nullable();
        }
    }

And this is the table schema:

CREATE TABLE [dbo].[Person](
    [ID] [bigint] IDENTITY(1,1) NOT NULL,
    [FirstName] [nvarchar](100) NOT NULL,
    [LastName] [nvarchar](100) NOT NULL,    
    [IsValid ] [bit] NOT NULL,
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]

When I insert a new Person in the database using Fluent NHibernate but we got this problem:

error dehydrating property value for `NameSpaceA.IsValid`

And inner exception:

_innerException = {"Invalid index 2 for this SqlParameterCollection with Count=2."}

Upvotes: 2

Views: 4458

Answers (2)

IWriteApps
IWriteApps

Reputation: 993

As Jamie Ide said, try explicitly mapping the Id as generated by identity, but also check and see if removing the .Not.Nullable() from the IsValid mapping allows it to work. Since in c# the type is boolean it can never be null anyway.

public PersonMap()
{
    Id(x => x.ID).GeneratedBy.Identity();
    Map(x => x.FirstName).Not.Nullable().Length(100);
    Map(x => x.LastName).Not.Nullable().Length(100);
    Map(x => x.IsValid);
}

Upvotes: 2

Jamie Ide
Jamie Ide

Reputation: 49261

Please provide the full error message and stack trace. This is an error message that is often misleading in that other errors occurred prior to this one.

I'll hazard a guess that the root cause is that you have not declared a generator for the ID property. The fact that it's typed as long may also be an issue. How are you generating/setting the ID property?

Assuming that you're using an identity column (int), the mapping would be:

Id(x => x.ID).GeneratedBy.Identity();

Upvotes: 0

Related Questions