这是我第一次使用NHibernate时,我从我的朋友那里得到了一个程序的源代码,之后程序运行良好,之后我正在尝试添加“Stock.hbm.xml”,如下所示:

`

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   namespace="NBooks.Core.Models"
                   assembly="NBooks.Core">

  <class name="Stock" table="Stocks" lazy="false">

    <id name="ID">
      <column name="Stock_ID" />
      <generator class="identity" />
    </id>

    <property name="Stock_name" column="Stock_name" />
   <property name="Comp_ID" column="Comp_ID" /> 
    <property name="Stock_Code" column="Stock_Code" />
    <property name="Address" column="Address" />
    <property name="Nots" column="Nots" />

  </class>

</hibernate-mapping>

`

与我的班级“Stock.cs”

 using System;
 using System.Collections.Generic;


 namespace NBooks.Core.Models
 {


public class Stock : BaseModel<Stock> 
{

         public virtual string Stock_name { get; set; }
         public virtual string Stock_Code { get; set; }
         public virtual int Comp_ID { get; set; }
         public virtual string Notes { get; set; }
         public virtual string Address { get; set; }
         public virtual bool Inactive { get; set; }

        public Stock()
         {
         }

         public Stock(string name)
         {
             this.Stock_name = name;
         }


 }


    public class StockEventArgs : EventArgs
    {

        public Stock Stock { get; set; }

        public StockEventArgs(Stock Stock)
        {
            this.Stock = Stock;
        }
 }

    public delegate void StockEventHandler(Stock sender, EventArgs e);

   }

基础模型是:

using System;
using System.Collections.Generic;
using NBooks.Core.Util;
using NBooks.Data.NHibernate;
using NHibernate;

namespace NBooks.Core.Models
{
public interface IBaseModel
{
    int Id { get; set; }
}

public class BaseModel<T> : IBaseModel
{
    IList<string> errors = new List<string>();
    public virtual int Id { get; set; }

    public virtual bool HasErrors {
        get { return errors.Count > 0; }
    }

    public virtual IList<string> Errors {
        get { return errors; }
    }

    public BaseModel()
    {
    }

    public virtual void Validate()
    {
        Errors.Clear();
    }

    public virtual void SaveOrUpdate()
    {
        ITransaction trans = null;
        try {
            ISession session = NHibernateHelper.OpenSession();
            trans = session.BeginTransaction();
            session.SaveOrUpdate(this);
            session.Flush();
            trans.Commit();
        } catch (Exception ex) {
            LoggingService.Error(ex.Message);
            MessageService.ShowError(ex.Message);
            trans.Rollback();
        }
    }

    public virtual void Delete()
    {
        ITransaction trans = null;
        try {
            ISession session = NHibernateHelper.OpenSession();
            trans = session.BeginTransaction();
            session.Delete(this);
            session.Flush();
            trans.Commit();
        } catch (Exception ex) {
            LoggingService.Error(ex.Message);
            MessageService.ShowError(ex.Message);
            trans.Rollback();
        }
    }

    public static T Read(int id)
    {
        return NHibernateHelper.OpenSession().Load<T>(id);
    }

    public static IList<T> FindAll()
    {
        return     
NHibernateHelper.OpenSession().CreateCriteria(typeof(T)).List<T>();
    }
}
}

当构建它时,每个东西都很好并且没有错误,当运行错误“NHibernate - 无法编译映射文档Stock.hbm.xml”时出现。 Thanx提前

===============>>#1 票数:1

您的映射中有拼写错误

<property name="Nots" column="Nots" />

应该

<property name="Notes" column="Nots" />

===============>>#2 票数:1 已采纳

我注意到你的XML中有一个拼写错误:

<property name="Nots" column="Nots" />

我建议你也考虑使用Fluent NHibernate 它是强类型的(大多数情况下),映射文件更易于阅读和使用lambda表达式,因此您不必使用XML路由。

您的映射将如下所示:

public class StockMap(): ClassMap<Stock>
{
    public StockMap(){  
        Id(x => x.Id).Column("Stock_ID").GeneratedBy.Identity();
        Map(x => x.Comp_ID);
        Map(x => x.Address);
        Map(x => x.Notes);
    }
}

  ask by user2284767 translate from so

未解决问题?本站智能推荐: