Search code examples
c#nhibernatefluent-nhibernate

C# Nhibernate query doesn't work


I just started learning C# and Nhibernate. I'm trying to solve following problem for hours.

Can anyone see the problem?

Table Line:

CREATE TABLE [dbo].[Line] 
(
    [ID]       UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
    [Name]     NVARCHAR (45)    NOT NULL,
    [ID_Color] UNIQUEIDENTIFIER NULL,

    PRIMARY KEY CLUSTERED ([ID] ASC),

    UNIQUE NONCLUSTERED ([Name] ASC),

    FOREIGN KEY ([ID_Color]) 
            REFERENCES [dbo].[Line_Color] ([ID]) ON DELETE SET NULL
);

Table Line_Color:

CREATE TABLE [dbo].[Line_Color] 
(
    [ID]    UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL,
    [Color] NVARCHAR (45)    NOT NULL,

    PRIMARY KEY CLUSTERED ([ID] ASC),
    UNIQUE NONCLUSTERED ([Color] ASC)
);

Entity.cs:

public class Entity<T> where T : Entity<T>
{
        public virtual Guid Id { get; set; }

        private int? _oldHashCode;

        public override Boolean Equals(object obj)
        {
            var other = obj as T;

            if (other == null)
                return false;

            // handle the case of comparing two NEW objects
            var otherIsTransient = Equals(other.Id, Guid.Empty);
            var thisIsTransient = Equals(Id, Guid.Empty);

            if (otherIsTransient && thisIsTransient)
                return ReferenceEquals(other, this);

            return other.Id.Equals(Id);
        }

        public override Int32 GetHashCode()
        {
            if (_oldHashCode.HasValue)
                return _oldHashCode.Value;

            var thisIsTransient = Equals(Id, Guid.Empty);

            if (thisIsTransient)
            {
                _oldHashCode = base.GetHashCode();
                return _oldHashCode.Value;
            }

            return Id.GetHashCode();
        }

        public static Boolean operator ==(Entity<T> x, Entity<T> y)
        {
            return Equals(x, y);
        }

        public static Boolean operator !=(Entity<T> x, Entity<T> y)
        {
            return !(x == y);
        }
}

Line.cs:

public class Line : Entity<Line>
{
        public virtual String Name { get; set; }
        public virtual Color Color { get; set; }
}

LineMap.cs:

public class LineMap : ClassMap<Line>
{
        public LineMap()
        {
            Id(x => x.Id);
            Map(x => x.Name);
            References(x => x.Color);
        }
}

Color.cs:

public class Color : Entity<Color>
{
        public virtual String ColorS { get; set; }
}

ColorMap.cs:

public class ColorMap : ClassMap<Color>
{
        public ColorMap()
        {
            Id(x => x.Id);
            Map(x => x.ColorS);
        }
}

On every query I do, I get something like

An unhandled exception of type 'NHibernate.Exceptions.GenericADOException' occurred in NHibernate.dll

Additional information: could not execute query

or NULL.


Solution

  • There are 2 problems in your code:

    • An entity is named Color and the corresponding table Line_Color;
    • A property is named ColorS and the corresponding column Color.

    They must correspond, or you have to tell NHibernate the DB names. Do some renaming (preferably) or adjust your mapping.

    ColorMap.cs:

    public class ColorMap : ClassMap<Color>
    {
            public ColorMap()
            {
                Table("Line_Color");
                Id(x => x.Id);
                Map(x => x.ColorS).Column("Color");
            }
    }