Nhibernate插入错误(GenericADOException)

时间:2013-04-03 17:32:14

标签: c# asp.net-mvc nhibernate model-view-controller

我是NHibernate的新手,所以我不确定我做错了什么。我目前收到此消息:

  

无法插入:   [NhibernateTest.Domain.Order#5ed0d146-644b-44ff-A080-f8cf3048158e] [SQL:   INSERT INTO Order(OrderName,ShippingAddress,ShippingDate,OrderID)   价值观(?,?,?,?)]

我正在尝试对我的数据库进行基本插入,该数据库只有一个表。

我的域名:

    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>

我的OrderRepository:

    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);
    }

我知道订单对象在传递给.Add-function时有信息。所以我认为它必须与我的映射有关?

由于

2 个答案:

答案 0 :(得分: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">

答案 1 :(得分: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)价值观(?,?,?,?,?,?,?,?,?,?,?,?)]

我有类似的东西   

<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"  />