Nhibernate出错:System.Data.SqlClient.SqlException:'Index'附近的语法不正确

时间:2010-07-22 00:32:06

标签: nhibernate fluent-nhibernate

我有一个nhibernate解决方案,我正在尝试保存,但是我收到了这个错误:

Test method HelloMusic.Core.Test.CrudTests.TestTrackAdd threw exception: 
NHibernate.Exceptions.GenericADOException: could not insert collection: [HelloMusic.BLL.Track.Credits#20][SQL: INSERT INTO Tracks_Credits (TrackID, Index, CreditID) VALUES (@p0, @p1, @p2)] ---> System.Data.SqlClient.SqlException: Incorrect syntax near 'Index'. If this is intended as a part of a table hint, A WITH keyword and parenthesis are now required. See SQL Server Books Online for proper syntax.

SQL如下:

NHibernate: SELECT this_.GenreID as GenreID9_0_, this_.GenreName as GenreName9_0_ FROM Genres this_
NHibernate: INSERT INTO Tracks (ContainsSamples, Description, HasExplicitLyrics, IsCover, Lyrics, Name, OrderIndex, GenreID) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7); select SCOPE_IDENTITY();@p0 = False, @p1 = 'Teh awesome18133437', @p2 = True, @p3 = False, @p4 = 'b'z in yer mouth18141375', @p5 = 'beez in yer mouth18141375', @p6 = 1, @p7 = 1
NHibernate: INSERT INTO Credits (Email, Location, Name, Role) VALUES (@p0, @p1, @p2, @p3); select SCOPE_IDENTITY();@p0 = 'foo@foo.com', @p1 = NULL, @p2 = 'Some Dood', @p3 = 'teh Awesums'
NHibernate: INSERT INTO Images (ForeignEntityID, ForeignEntityType, CreatedDate, Extension, FileName, Height, IsOriginal, LastModifiedDate, LocationPath, MetaData, SizeInKiloBytes, Type, Width) VALUES (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10, @p11, @p12); select SCOPE_IDENTITY();@p0 = 0, @p1 = 'Track', @p2 = 7/21/2010 5:20:59 PM, @p3 = 'jpregg', @p4 = 'tex0r', @p5 = 0, @p6 = True, @p7 = 7/21/2010 5:21:00 PM, @p8 = '//server/yourmom', @p9 = NULL, @p10 = 5898, @p11 = NULL, @p12 = 0
NHibernate: INSERT INTO Languages (Name) VALUES (@p0); select SCOPE_IDENTITY();@p0 = 'Spanglish'
NHibernate: INSERT INTO Tracks_Credits (TrackID, Index, CreditID) VALUES (@p0, @p1, @p2);@p0 = 20, @p1 = 0, @p2 = 19

我怀疑问题就是这个(我在SQL中运行它并得到相同的错误):

NHibernate: INSERT INTO Tracks_Credits (TrackID, Index, CreditID) VALUES (@p0, @p1, @p2);@p0 = 20, @p1 = 0, @p2 = 19

但是我在这里抛出的是该表没有“索引”列

我的地图中的任何地方都没有“索引”。有没有人见过这个?

为什么要在那里抛出'指数'?

这是信用图:

public class CreditMap: ClassMap<Credit>
{
    public CreditMap()
    {
        Table("Credits");
        Id(x => x.ID, "CreditId");
        Map(x => x.Email, "Email")
            .Length(1000);
        Map(x => x.Location, "Location")
            .Length(1000);
        Map(x => x.Name, "Name")
            .Length(1000);
        Map(x => x.Role, "Role")
            .Length(1000);
    }
}

这是轨道地图:

public class ForeignEntityTypeFilter : FilterDefinition
{
    public ForeignEntityTypeFilter()
    {
        WithName("ForeignEntity")
            .AddParameter("IsType", NHibernate.NHibernateUtil.String);
    }
}

public class TrackMap: ClassMap<Track>
{
    public TrackMap()
    {
        Table("Tracks");
        Id(x => x.ID, "TrackId");
        Map(x => x.ContainsSamples);
        Map(x => x.Description);
        Map(x => x.HasExplicitLyrics);
        Map(x => x.IsCover);
        Map(x => x.Lyrics);
        Map(x => x.Name)
            .Length(1000);
        Map(x => x.OrderIndex);
        References<Genre>(x => x.Genre, "GenreID");
        HasManyToMany<Credit>(x => x.Credits)
            .ChildKeyColumn("CreditID")
            .AsList()
            .ParentKeyColumn("TrackID")
            .Table("Tracks_Credits")
            .Not.Inverse()
            .Cascade.SaveUpdate();
        HasMany<TrackImage>(x => x.Images)
            .Table("Images")
            .KeyColumn("ForeignEntityID")
            .ApplyFilter<ForeignEntityTypeFilter>("'Track' == ForeignEntityType")
            .Not.Inverse()
            .Cascade.SaveUpdate();
        HasManyToMany<Language>(x => x.Languages)
            .ChildKeyColumn("LanguageID")
            .AsList()
            .ParentKeyColumn("TrackID")
            .Table("Tracks_Languages")
            .Not.Inverse()
            .Cascade.SaveUpdate();
        HasManyToMany<MediaFile>(x => x.MediaFiles)
            .ChildKeyColumn("MediaFileID")
            .AsList()
            .ParentKeyColumn("TrackID")
            .Table("Tracks_MediaFiles")
            .Not.Inverse()
            .Cascade.SaveUpdate();
    }
}

2 个答案:

答案 0 :(得分:14)

问题是集合的AsList()映射。这将集合映射为有序列表,该列表需要数据库中的索引列来维护订单。您可能希望使用AsBag()映射它们。

答案 1 :(得分:0)

如果您正在使用自动化并且已经执行覆盖以创建多对多关系,则需要添加以设置列的索引名称,例如:

public class ManytoManyConvention: IHasManyToManyConvetion
{
   public void Apply(IManyToManyCollectionInstance instance)
   {
      instance.Index.Column("Index1");
   }
}

这会创建一个这样的表:

create table Table1ToTable2 (
       Table1_id INT not null,
       Table2_id INT not null,
       Index1 INT not null,
       primary key (Table1_id, Index1)
    )