【Nhibernate】入门 踩雷篇

总结(喜欢写在前面,记性不好老忘记解决问题时的思路):

使用框架一般不会完整的看文档,直接上来就搞,踩雷是必须的,重要的是遇到雷的时候要快速变换思路,是不是姿势不对(文件位置不对) 提高解决问题的速度。

官网的文章可信度还是挺高的,尽量的不要跑偏。

沿着官网 –> http://nhibernate.info/doc/tutorials/first-nh-app/your-first-nhibernate-based-application.html

开始coding

刚开始map放在 application下报:

No persister for: 

image

单独的把model建一个项目

报:

Could not compile the mapping document

原因是我在解决问题的时候加了   <mapping assembly="HibernateService"/>

image

image

保留一处就好了

又出现下面问题:

image

  public class Product
    {
        public virtual Guid Id { get; set; }
        public virtual string Name { get; set; }
        public virtual string Category { get; set; }
        public virtual bool Discontinued { get; set; }
    }

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
                   assembly="HibernateService"
                   namespace="HibernateService">
  <class name="Product" table="Product">
    <id name="Id">
      <generator class="guid" />
    </id>
    <property name="Name" />
    <property name="Category" />
    <property name="Discontinued" />
  </class>
  <!-- more mapping info here -->

</hibernate-mapping>

guid要小写

image

写入成功!

image

原文地址:https://www.cnblogs.com/viewcozy/p/4639398.html