Unable to cast object of type 'System.Int32' to type 'System.String'

62 views
Skip to first unread message

Antonio Kuroki

unread,
Feb 15, 2017, 9:16:11 AM2/15/17
to Fluent NHibernate
I am tring to do a sample exemple using Fluent NHibernate but i am having some dificults

this is my code:

 public class Categoria
    {
        public virtual int Id { get; protected set; }
        public virtual string Descricao { get; set; }
    }

public class CategoriaMap : ClassMap<Categoria>
    {
        public CategoriaMap()
        {
            //Table("Categoria");
            Id(x => x.Id);//.Column("Id").CustomType("System.Int32").GeneratedBy.Identity();
            Map(x => x.Descricao);
        }  
    }


here is the insert method on my DAO:

 public string incluir(Categoria entidade)
            {
                String resultado = null;
                ISession session = null;
                ITransaction transaction = null;

                try
                {
                    session = HibernateHelper.OpenSession();
                    transaction = session.BeginTransaction();

                    resultado = (string)session.Save(entidade);
                    transaction.Commit();
                }
                catch (Exception e)
                {
                    resultado = e.Message;
                    transaction.Rollback();
                }
                finally
                {
                    if (transaction.IsActive && !transaction.WasCommitted)
                    {
                        transaction.Commit();
                    }
                }


                return resultado;
            }

my table script:

CREATE TABLE [dbo].[Categoria](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Descricao] [varchar](50) NULL,
 CONSTRAINT [PK_Categoria] 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 run my project, i receive this erro:

NHibernate: INSERT INTO [Categoria] (Descricao) 
VALUES (@p0); select SCOPE_IDENTITY();
@p0 = 'Categoria A' [Type: String (4000)]
Unable to cast object of type 'System.Int32' to type 'System.String'.


Does anybody knows how to resolve it?


Brandon Perry

unread,
Feb 15, 2017, 9:35:33 AM2/15/17
to fluent-n...@googlegroups.com
Why do you believe you need to add .CustomType("System.Int32”) to your map? This should automatically Just Work without specifying the type.



On Feb 15, 2017, at 8:16 AM, Antonio Kuroki <kuro...@gmail.com> wrote:

.CustomType("System.Int32")



signature.asc

Antonio Kuroki

unread,
Feb 15, 2017, 11:39:56 AM2/15/17
to Fluent NHibernate

Hi Brandon,

I try to set the customType when i had this error but it did not work so.

It doenst mather if i set or not the customType, i always have this error.
Reply all
Reply to author
Forward
0 new messages