流利的NHibernate和SQLite:无法保存任何内容

时间:2012-09-02 17:49:59

标签: c# sqlite fluent-nhibernate

我是NHibernate(以及一般的ORM)的新手,这是我第一次尝试使用它,所以我完全迷失了从哪里开始寻找这个工作。如果我使用“原始”SQL向数据库添加条目,我可以使用NHibernate检索它们,但保存实体似乎总是失败

  

NHibernate.StaleStateException:意外的行数:0;预期:1

映射类:

class AAniDBFileMap : ClassMap<AAniDBFile>
{
    public AAniDBFileMap()
    {
        Table("FILE_VIEW");

        Id(x => x.FID)
            .Column("FID");

        Map(x => x.AID);
        Map(x => x.EID);
        Map(x => x.GID);
        Map(x => x.IsDeprecated)
            .Column("DEPRECATED");
        Map(x => x.State)
            .CustomType<AniDBFile.StateMask>();
        Map(x => x.Size);
        Map(x => x.ED2K);
        Map(x => x.VideoColorDepth)
            .Column("VIDEO_COLOR_DEPTH");
        Map(x => x.Source);
        Map(x => x.VideoCodec)
            .Column("VIDEO_CODEC");
        Map(x => x.VideoBitrate)
            .Column("VIDEO_BITRATE");
        Map(x => x.VideoResolution)
            .Column("VIDEO_RESOLUTION");
        Map(x => x.Length)
            .Column("FILE_LENGTH");
        Map(x => x.AiredDate)
            .Column("AIRED");
        Map(x => x.TotalEpisodes)
            .Column("EPISODES");
        Map(x => x.HighestEpisodeNumber)
            .Column("HIGHEST_EPISODE_NO");
        Map(x => x.Year)
            .Column("AIR_YEAR");
        Map(x => x.Type)
            .Column("ANIME_TYPE");
        Map(x => x.RomanjiName)
            .Column("ROMANJI_NAME");
        Map(x => x.KanjiName)
            .Column("KANJI_NAME");
        Map(x => x.EnglishName)
            .Column("ENGLISH_NAME");
        Map(x => x.EpNo)
            .Column("EPISODE_NO");
        Map(x => x.EpName)
            .Column("EPISODE_ENGLISH_NAME");
        Map(x => x.EpRomanjiName)
            .Column("EPISODE_ROMANJI_NAME");
        Map(x => x.EpKanjiName)
            .Column("EPISODE_KANJI_NAME");
        Map(x => x.EpisodeRating)
            .Column("EPISODE_RATING");
        Map(x => x.EpisodeVoteCount)
            .Column("EPISODE_VOTES");
        Map(x => x.GroupName)
            .Column("GROUP_NAME");
        Map(x => x.GroupShortName)
            .Column("GROUP_SHORT_NAME");

        HasMany(x => x.AudioCodecs)
            .Table("AUDIO_TRACK")
            .KeyColumn("FID")
            .Cascade.All()
            .Element("AUDIO_CODEC")
            .AsList(index => index.Column("TRACK_INDEX"));

        HasMany(x => x.AudioBitrates)
            .Table("AUDIO_TRACK")
            .KeyColumn("FID")
            .Cascade.All()
            .Element("AUDIO_BITRATE")
            .AsList(index => index.Column("TRACK_INDEX"));

        HasMany(x => x.DubLanguage)
            .Table("AUDIO_TRACK")
            .KeyColumn("FID")
            .Cascade.All()
            .Element("LANGUAGE")
            .AsList(index => index.Column("TRACK_INDEX"));

        HasMany(x => x.SubLanguage)
            .Table("SUBTITLE_TRACK")
            .KeyColumn("FID")
            .Cascade.All()
            .Element("LANGUAGE")
            .AsList(index => index.Column("TRACK_INDEX"));

        HasMany(x => x.RelatedAIDList)
            .Table("RELATED_ANIME")
            .KeyColumn("AID")
            .PropertyRef("AID")
            .Cascade.All()
            .Element("AID_OTHER")
            .AsList(index => index.Column("AID_INDEX"));

        HasMany(x => x.RelatedAIDType)
            .Table("RELATED_ANIME")
            .KeyColumn("AID")
            .PropertyRef("AID")
            .Cascade.All()
            .Element("RELATION_TYPE")
            .AsList(index => index.Column("AID_INDEX"));

        HasMany(x => x.ShortNameList)
            .Table("ANIME_SHORT_NAME")
            .KeyColumn("AID")
            .PropertyRef("AID")
            .Cascade.All()
            .Element("SHORT_NAME")
            .AsBag();

        HasMany(x => x.SynonymList)
            .Table("ANIME_SYNONYM")
            .KeyColumn("AID")
            .PropertyRef("AID")
            .Cascade.All()
            .Element("SYNONYM")
            .AsBag();
    }
}

Hibernate配置:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
      <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="connection.connection_string_name">AAniDB.Properties.Settings.dbConnectionString</property>
      <property name="query.substitutions">true 1, false 0</property>
      <property name="show_sql">true</property>
    </session-factory>
  </hibernate-configuration>

我得到例外:

var cfg = new NHibernate.Cfg.Configuration();

cfg.Configure();

var session = Fluently.Configure(cfg)
    .Mappings(m => m.FluentMappings
    .AddFromAssemblyOf<Program>())
    .BuildSessionFactory()
    .OpenSession();

using (var transaction = session.BeginTransaction())
{
    session.Save(new AAniDBFile((AniDBFile)_referenceFile));
    transaction.Commit();
}

0 个答案:

没有答案