Reputation: 53
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.
Upvotes: 0
Views: 771
Reputation: 131
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.
Upvotes: 0
Reputation: 1255
There are 2 problems in your code:
Color
and the corresponding table Line_Color
;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");
}
}
Upvotes: 1