C# Nhibernate 查询不起作用

C# Nhibernate query doesn't work

我刚开始学习 C# 和 Nhibernate。我试图解决以下几个小时的问题。

谁能看出问题所在?

Table :

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);
        }
}

在我执行的每个查询中,我都会得到类似

的信息

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

Additional information: could not execute query

或 NULL。

您的代码中有 2 个问题:

  • 一个实体名为Color和对应的table Line_Color;
  • 一个属性被命名为ColorS和对应的列Color.

它们必须对应,否则你必须告诉 NHibernate 数据库名称。进行一些重命名(最好)或调整映射。

ColorMap.cs:

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

实体代表 table 个(或 table 个赞)。

"color" 不需要 table(和 entity/map)。 "Color".

不需要 class

线路需要 属性 和地图 属性 "color"。

Hibernate 指责的问题是因为它无法将实体颜色与任何 table.

相匹配