1

我是 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 函数时具有信息。所以我认为它一定与我的映射有关?

谢谢

4

2 回答 2

2

Order是保留关键字。例如,在查询您使用的东西时Order by。以下是所有保留字的列表:http: //msdn.microsoft.com/en-us/library/aa238507%28v=sql.80%29.aspx

您只需更改表名和映射即可解决此问题。例如,您可以命名您的表Orders

<class name="Order" table="Orders" lazy="true">
于 2013-04-03T18:02:36.203 回答
0

我收到如下错误:无法插入:[RegEmails.Model.RegInfo][SQL: INSERT INTO reg_info (MESSAGE_HASH, IS_INTERNAL, PRODUCT, SERVER, VERSION, MODULE, BRIDGE_LICENCE, BRIDGE_LICENCE_EXPIRY, BACK_OFFICE_LICENCE, BACK_OFFICE_LICENCE_EXPIRY, COMMENT, CREATE_DATE) VALUES (? , ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)]

我有类似的东西

<id name="info_id" column="INFO_ID">
  <generator class="identity" />
</id>
<property name="MessageHash" column="MESSAGE_HASH" length="512" />
<property name="IsInternal" column="IS_INTERNAL" />
<property name="Product" column="PRODUCT" />
<property name="Server" column="SERVER" />
<property name="Version" column="VERSION" />
<property name="Module" column="MODULE" />
<property name="BridgeLicence" column="BRIDGE_LICENCE" />
<property name="BridgeLicenceExpiry" column="BRIDGE_LICENCE_EXPIRY" />
<property name="BackofficeLicence" column="BACK_OFFICE_LICENCE" />
<property name="BackofficeLicenceExpiry" column="BACK_OFFICE_LICENCE_EXPIRY" />
<property name="Comment" column="COMMENT" />
<property name="CreateDate" column="CREATE_DATE" />

InnerException:“字段列表”中的未知列“MESSAGE_HASH”

我通过删除解决了

<id name="info_id" column="INFO_ID">
  <generator class="identity" />
</id>
<property name="MessageHash"  length="512" />
<property name="IsInternal"  />
<property name="Product" />
<property name="Server"  />
<property name="Version" />
<property name="Module"  />
<property name="BridgeLicence"  />
<property name="BridgeLicenceExpiry" />
<property name="BackofficeLicence" />
<property name="BackofficeLicenceExpiry"  />
<property name="Comment" />
<property name="CreateDate"  />

于 2014-01-27T13:30:22.400 回答