0

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.

2
  • 1
    Usually there should be an Inner Exception (if it failed executing a query), which gives details of the query it was triyng to execute, do you have this in your case? Commented Sep 10, 2016 at 15:35
  • It says {"Invalid Object \" Color \ "."} @starlight54 Commented Sep 10, 2016 at 15:55

2 Answers 2

1

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");
        }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Mapping the table is always a good idea to keep mapping clear and neat.
0

Entities represents tables (or table likes).

You don't need an table (and entity/map) for "color". You don't need to have a class for "Color".

Line need to have a property and a map for property "color".

The problem Hibernate acuses is because it can't match the Entity color to any table.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.