我是 NHibernate 的新手,所以我不确定我做错了什么。我目前收到此消息:
无法插入:[NhibernateTest.Domain.Order#5ed0d146-644b-44ff-a080-f8cf3048158e][SQL: INSERT INTO Order (OrderName, ShippingAddress, ShippingDate, OrderID) VALUES (?, ?, ?, ?)]
我正在尝试对我的数据库进行基本插入,该数据库只有一个表。
我的域类:
public class Order
{
public virtual Guid OrderID { get; set; }
public virtual string Name { get; set; }
public virtual string ShipAddress { get; set; }
public virtual string ShipDate { get; set; }
public Order()
{
}
public Order(string name, string shipAddress, string shipDate)
{
Name = name;
ShipAddress = shipAddress;
ShipDate = shipDate;
}
}
还有我的映射:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NhibernateTest" namespace="NhibernateTest.Domain">
<class name="Order" table="Order" lazy="true">
<id name="OrderID" column="OrderID">
<generator class="guid" />
</id>
<property name="Name" column="OrderName" type="string" />
<property name="ShipAddress" column="ShippingAddress" type="string" />
<property name="ShipDate" column="ShippingDate" type="string" />
</class>
</hibernate-mapping>
我的订单库:
public void Add(Order order)
{
/* Open up a session to the database. */
using (ISession session = NHibernateHelper.OpenSession())
{
/* Open up a transaction to the database since an order object will be added. */
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(order);
transaction.Commit();
}
}
}
这是我的 AddMethod 以及我的 loadCfg 函数:
[HttpPost]
public ActionResult AddOrder(FormCollection form)
{
LoadNHibernateCfg();
OrderRepository repo = new OrderRepository();
var order = new Order { Name = form[0], ShipAddress = form[1], ShipDate = form[2] };
//order.Name = form[0];
//order.ShipAddress = form[1];
//order.ShipDate = form[2];
repo.Add(order);
return Index();
}
public static void LoadNHibernateCfg()
{
var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof (Order).Assembly);
new SchemaExport(cfg).Execute(true, false, true);
}
我知道 Order 对象在传递给 .Add 函数时具有信息。所以我认为它一定与我的映射有关?
谢谢