我正在使用nHibernate并尝试添加一些主细节数据。数据库图表附在此处。
以下是添加类别和两个产品的代码:
//Category
namespace Sample.CustomerService.Domain
{
public class Category {
public Category() {
Products = new List<Product>();
}
public virtual int CategoryID { get; set; }
public virtual IList<Product> Products { get; set; }
public virtual string Name { get; set; }
public virtual string Unit { get; set; }
public virtual void AddProduct(Product product)
{
Products.Add(product);
}
}
}
//CategoryMap
namespace Sample.CustomerService.Domain
{
public class CategoryMap : ClassMap<Category> {
public CategoryMap() {
Table("Category");
LazyLoad();
Id(x => x.CategoryID).GeneratedBy.Identity().Column("CategoryID");
Map(x => x.Name).Column("Name").Not.Nullable().Length(50);
Map(x => x.Unit).Column("Unit").Not.Nullable().Length(3);
HasMany(x => x.Products).KeyColumn("CategoryID");
}
}
}
//--------------------------------------------------------------------------------------------
//Product
namespace Sample.CustomerService.Domain
{
public class Product {
public Product() {
}
public virtual int ProductID { get; set; }
public virtual Category Category { get; set; }
public virtual string Name { get; set; }
public virtual decimal UnitPrice { get; set; }
}
}
//ProductMap
namespace Sample.CustomerService.Domain {
public class ProductMap : ClassMap<Product> {
public ProductMap() {
Table("Product");
LazyLoad();
Id(x => x.ProductID).GeneratedBy.Identity().Column("ProductID");
References(x => x.Category).Column("CategoryID");
Map(x => x.Name).Column("Name").Not.Nullable().Length(50);
Map(x => x.UnitPrice).Column("UnitPrice").Not.Nullable();
}
}
}
//----------------------------------------------------------------------------------------------
//Program
namespace WindowsHibernateTest
{
public partial class TestClass : Form
{
public TestClass()
{
InitializeComponent();
CreateNewProduct();
}
public void CreateNewProduct()
{
try
{
var sessionFactory = CreateSessionFactory();
using (var session = sessionFactory.OpenSession())
{
using (var sqlTrans = session.BeginTransaction())
{
Category newGold = new Category() { Name = "Gold", Unit = "GRM" };
Product ngOrn = new Product() { Name = "Bangles", UnitPrice = 1000.10M };
Product ogOrn = new Product() { Name = "Rings", UnitPrice = 2000.10M };
AddProductsToCategory(newGold, ngOrn, ogOrn);
session.SaveOrUpdate(newGold);
sqlTrans.Commit();
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure().Database(MsSqlConfiguration.MsSql2005.
ConnectionString("Data Source=MyDB; Initial Catalog=NHibernateTest; Trusted_Connection=true;")).Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>()).BuildSessionFactory();
}
public static void AddProductsToCategory(Category category, params Product[] products)
{
foreach (var product in products)
{
category.AddProduct(product);
}
}
}
}
在第sqlTrans.Commit()
行中,我收到以下错误:
对象引用未保存的瞬态实例 - 保存瞬态 冲洗前的实例。类型:Sample.CustomerService.Domain.Product, 实体:Sample.CustomerService.Domain.Product
我已经谷歌搜索了一段时间,并没有得到令人信服的解决方案。我已尝试this,this和this,但无法解决此错误。请帮我解决这个错误。提前谢谢。
答案 0 :(得分:5)
我认为您需要在保存类别之前保存产品,因为您在HasMany上没有Cascade。 您需要更改添加产品方法以保持关系
foreach (var product in products)
{
category.AddProduct(product);
product.Category = category;
}