Fluent nhibernate Fluent NHibernate自动映射抛出“;StaleStateException“;尝试提交列表时<&燃气轮机;

Fluent nhibernate Fluent NHibernate自动映射抛出“;StaleStateException“;尝试提交列表时<&燃气轮机;,fluent-nhibernate,one-to-many,automapping,Fluent Nhibernate,One To Many,Automapping,提交Order.OrderItems属性(IList)时,以下代码引发StaleStateException异常。例外情况全文如下: 在NHibernate.dll中发生类型为“NHibernate.StaleStateException”的未处理异常 其他信息:意外的行计数:0;预期:1 我刚刚开始使用NHibernate,这对我来说毫无意义。有人能解释一下怎么回事吗 大部分代码附加在下面。很抱歉这么多,但我觉得这比忽略一些重要的事情要好 如果我注释掉行OrderItems=OrderItem

提交Order.OrderItems属性(IList)时,以下代码引发StaleStateException异常。例外情况全文如下:

在NHibernate.dll中发生类型为“NHibernate.StaleStateException”的未处理异常

其他信息:意外的行计数:0;预期:1

我刚刚开始使用NHibernate,这对我来说毫无意义。有人能解释一下怎么回事吗

大部分代码附加在下面。很抱歉这么多,但我觉得这比忽略一些重要的事情要好

如果我注释掉行OrderItems=OrderItems,其他一切都正常

using System;
using System.Collections.Generic;
using System.IO;
using AutomappingSample.Domain;
using FluentNHibernate.AutoMap;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;

namespace AutomappingSample
{
    class Program
    {
        private const string DbFile = "AutomappingSample.db";
        private const bool useSqlServerCe = true;

        static void Main()
        {
            var factory = CreateSessionFactory();
            using (var session = factory.OpenSession())
            using(var tx = session.BeginTransaction())
            {
                var product1 = new Product
                                   {
                                       Name = "Apples",
                                       UnitPrice = 4.5m,
                                       Discontinued = true
                                   };
                var product2 = new Product
                                   {
                                       Name = "Pears",
                                       UnitPrice = 3.5m,
                                       Discontinued = false
                                   };
                session.Save(product1);
                session.Save(product2);

                var customer = new Customer
                                   {
                                       FirstName = "John",
                                       LastName = "Doe",
                                   };
                session.Save(customer);

                var orderItems = new List<OrderItem>
                                     {
                                         new OrderItem {Id = 1, Quantity = 100, Product = product1},
                                         new OrderItem {Id = 2, Quantity = 200, Product = product2},
                                     };
                var order = new Order()
                                {
                                    Customer = customer, 
                                    Id = 1, 
                                    OrderDate = DateTime.Now, 

                                    // CAUSES FOLLOWING EXCEPTION WHEN COMMIT:
                                    //      An unhandled exception of type 'NHibernate.StaleStateException' 
                                    //      occurred in NHibernate.dll
                                    //
                                    //      Additional information: Unexpected row count: 0; expected: 1
                                    OrderItems = orderItems
                                };
                session.Save(order);

                // EXCEPTION IS THROWN HERE
                tx.Commit();

          }

            Console.WriteLine("Hit enter to exit...");
            Console.ReadLine();
        }

        private static ISessionFactory CreateSessionFactory()
        {
            IPersistenceConfigurer persistenceConfigurer;
            if (useSqlServerCe)
                persistenceConfigurer =
                    MsSqlCeConfiguration.Standard.ConnectionString(c => c.Is("Data Source=AutomappingSample.sdf"));
            else
                persistenceConfigurer = SQLiteConfiguration.Standard.UsingFile(DbFile);

            return Fluently.Configure()
                .Database(persistenceConfigurer)
                .Mappings(m => m.AutoMappings.Add(
                                   AutoPersistenceModel
                                       .MapEntitiesFromAssemblyOf<Customer>()
                                       .WithSetup(s => { s.IsBaseType = type => type == typeof (EntityBase); })
                                       .Where(t => t.Namespace.EndsWith("Domain"))
                                       .ConventionDiscovery.Add<MyStringLengthConvention>()
                                       .ConventionDiscovery.Add<MyIdConvention>()
                                       .ConventionDiscovery.Add<MyForeignKeyConvention>()
                                   ))
                .ExposeConfiguration(BuildSchema)
                .BuildSessionFactory();
        }

        private static void BuildSchema(Configuration config)
        {
            // delete the existing db on each run (only for SQLite)
            if (File.Exists(DbFile))
                File.Delete(DbFile);

            // this NHibernate tool takes a configuration (with mapping info in)
            // and exports a database schema from it
            new SchemaExport(config)
                .Create(true, true);
        }
    }
}


您可能需要告诉它将保存级联到Order上的OrderItems

大概是这样的: (来自)

.Override(映射=>
{
HasMany(x=>x.OrderItems)
.Cascade.All();
});

您需要先保存
订单项
,然后再尝试保存
订单

session.Save(orderItems[0]);
session.Save(orderItems[1]);
session.Save(order);

自从发布这个问题以来,我了解到获得级联保存的最简单方法是添加一个DefaultCascade约定

请参阅下面代码中启动“var autoPersistanceModel=…”的部分

私有静态ISessionFactory CreateSessionFactory()
{
ISessionFactory sessionFactory=null;
//自动映射的XML文件将导出到项目的
//…\bin\x86\Debug\automaexport目录
//请参阅下面的“.ExportTo()”
常量字符串automaexportdir=“automaexport”;
如果(!Directory.Exists(autoMapExportDir))
CreateDirectory(autoMapExportDir);
尝试
{
var autoPersistenceModel=
自动映射
//仅映射DlsAppAutomapped命名空间中的实体
.Where(t=>t.Namespace==“DlsAppAutomapped”)
//在所有实体上进行级联保存,以便保存列表
//将自动保存
.Conventions.Add(DefaultCascade.All())
;
sessionFactory=fluntly.Configure()
.Database(SQLiteConfiguration.Standard
.UsingFile(DbFile)
//在控制台上显示生成的SQL
.ShowSql()
)
.Mappings(m=>m.AutoMappings.Add(autoPersistenceModel)
//将XML映射文件保存到此目录
.ExportTo(自动出口目录)
)
.ExposeConfiguration(构建架构)
.BuildSessionFactory()
;
}
捕获(例外e)
{
Debug.WriteLine(e);
}
返回工厂;
}

@Derek-在哪里添加此代码???这是我第一次接触“fluent”接口,我很难确定在哪里可以调用某些方法。有没有一个文档在一个地方列出了所有Fluent NHibernate类和方法?Oops遗漏了这一部分!它位于AutoPersistenceModel类上。此部分:AutoPersistenceModel.MapEntitiesFromAssemblyOf()虽然我认为这是一种较旧的语法,但在我使用的版本(几天前更新)上,它是AutoMap.AssemblyOf(),因此我不确定是否还有其他差异。您可能想尝试的另一个方法是向OrderItems添加Order属性,FNH非常聪明,可以选择该链接,然后自动添加级联。。无论如何值得一试。@Derek-我的示例使用了旧语法,你是对的。这是我能找到的唯一一个工作示例,但结果证明它使用了Fluent NHibernate的旧版本。我试图用新语法将其转换为最新版本,但没有成功。我再次遇到异常,错误消息对我来说绝对是胡言乱语。我的印象是,自动映射工作时很酷,但不工作时很难排除故障。很抱歉听到您遇到问题。我发现只要按照我链接到的页面上的示例就可以很容易地开始工作。如果您在生成映射时遇到问题,那么您可以随时将它们写入磁盘以查看它在做什么。有时,FNH做出的假设与我们做出的假设不同。要写出文件,可以在AutoPersistenceModel类上调用WriteMappingsTo。这是有效的(一个有效的解决方案,因此为+1),但我更愿意使用持久性模型,如@tom bushell的answerTested中所述,根据我的需要进行调整,并像一个符咒一样工作。太糟糕了,我不能多次否决你的答案!很高兴我能帮忙。默认约定是一个很好的FNH特性,似乎很少有人知道。
using System;
using System.Collections.Generic;

namespace AutomappingSample.Domain
{
    public class Order : EntityBase
    {
        public virtual DateTime OrderDate { get; set; }
        public virtual Customer Customer { get; set; }
        public virtual IList<OrderItem> OrderItems { get; set; }
    }
}
namespace AutomappingSample.Domain
{
    public class OrderItem : EntityBase
    {
        public virtual int Quantity { get; set; }
        public virtual Product Product { get; set; }
    }
}
.Override<Order>(map =>
{
  map.HasMany(x => x.OrderItems)
    .Cascade.All();
});
session.Save(orderItems[0]);
session.Save(orderItems[1]);
session.Save(order);
    private static ISessionFactory CreateSessionFactory()
    {
        ISessionFactory sessionFactory = null;

        // Automapped XML files will be exported to project's
        // ...\bin\x86\Debug\AutoMapExport directory
        // See ".ExportTo()" below
        const string autoMapExportDir = "AutoMapExport";
        if( !Directory.Exists(autoMapExportDir) )
            Directory.CreateDirectory(autoMapExportDir);

        try
        {
            var autoPersistenceModel = 
                AutoMap.AssemblyOf<DlsAppOverlordExportRunData>()
                        // Only map entities in the DlsAppAutomapped namespace
                       .Where(t => t.Namespace == "DlsAppAutomapped")
                       // Do cascading saves on all entities so lists 
                       // will be automatically saved 
                       .Conventions.Add( DefaultCascade.All() )
                ;

            sessionFactory = Fluently.Configure()
                .Database(SQLiteConfiguration.Standard
                              .UsingFile(DbFile)
                              // Display generated SQL on Console
                              .ShowSql()
                         )
                .Mappings(m => m.AutoMappings.Add(autoPersistenceModel)
                                             // Save XML mapping files to this dir
                                             .ExportTo(autoMapExportDir)
                         )
                .ExposeConfiguration(BuildSchema)
                .BuildSessionFactory()
                ;
        }
        catch (Exception e)
        {
            Debug.WriteLine(e);
        }

        return sessionFactory;
    }