Fluent NHibernate AutoMapping 抛出“StaleStateException”当尝试提交列表时<>
当 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);
}
}
}
namespace AutomappingSample.Domain
{
public class EntityBase
{
public virtual int Id { get; set; }
}
}
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; }
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在尝试保存
订单
之前,您需要先保存orderItems
:You need to first save
orderItems
before attempting to save theorder
:自从发布问题以来,我了解到获得级联保存的最简单方法是添加 DefaultCascade 约定。
请参阅下面代码中以“var autoPersistanceModel = ...”开头的部分
Since posting the question, I've learned that the easiest way to get cascading saves is to add a DefaultCascade convention.
See the section in the code below that starts "var autoPersistanceModel = ..."
您可能需要告诉它将保存级联到订单上的订单项。
像这样的东西:
(来自此处)
You may need to tell it to cascade the saves to the OrderItems on Order.
Something like this:
(from here)