NHibernate无法返回实体id属性

时间:2013-06-03 11:07:54

标签: c# .net nhibernate orm

这里我使用HBM和FluentNHibernate的方式映射实体, 但为什么HBM不会自动分配实体的ID?流利的api可以..

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
  namespace="XmlMappingSimple.Domain"
  assembly="XmlMappingSimple">

  <class name="Customer" table="t_Customers" schema="dbo">
    <id  column="Id"  type="Int32" >
      <generator class="identity"></generator>
    </id>
    <property name="CustomerName" length="50" type="string"  not-null="true">
    </property>
  </class>
</hibernate-mapping>

这是我所有的c#代码:

using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Mapping;
using NHibernate.Cfg;
using NHibernate.Tool.hbm2ddl;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XmlMappingSimple.Domain;

namespace XmlMappingSimple
{
    public class CustomerMap : ClassMap<Customer>
    {
        public CustomerMap()
        {
            Id(_ => _.Id)
                .GeneratedBy.Identity();

            Map(_ => _.CustomerName);
            Table("t_Customers");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var sessionFactory = new Configuration()
                .Configure()
                .AddAssembly(typeof(Customer).Assembly)
                .BuildSessionFactory();


            using (var session = sessionFactory.OpenSession())
            {
                var cus1 = new Customer { CustomerName = "my name1" };
                var cus2 = new Customer { CustomerName = "my name2" };

                session.Persist(cus1);
                session.Flush();

                session.Save(cus2);
                session.Flush();

                Console.WriteLine("cus1.Id = {0} ", cus1.Id);
                Console.WriteLine("cus2.Id = {0} ", cus2.Id);
            }

            var sessionFactoryForFluent = Fluently.Configure()
                 .Database(MsSqlConfiguration.MsSql2008
                .ConnectionString(@"Server=.\sql2008;initial catalog=nhibernate;Integrated Security=True"))
                .Mappings(_ => _.FluentMappings.AddFromAssemblyOf<Customer>())
                .BuildConfiguration().BuildSessionFactory();

            Console.WriteLine("Fluent ================");
            using (var session = sessionFactoryForFluent.OpenSession())
            {
                var cus1 = new Customer { CustomerName = "my name1" };
                var cus2 = new Customer { CustomerName = "my name2" };

                session.Persist(cus1);
                session.Flush();

                session.Save(cus2);
                session.Flush();

                Console.WriteLine("cus1.Id = {0} ", cus1.Id);
                Console.WriteLine("cus2.Id = {0} ", cus2.Id);
            }

        }
    }
}

这是控制台输出:

cus1.Id = 0
cus2.Id = 0
Fluent ================
cus1.Id = 29
cus2.Id = 30

我知道session.Save(cus2)会返回新的身份ID,我知道session.Save (cus2)会返回新的身份ID,但entity.Id也应该自动分配啊

1 个答案:

答案 0 :(得分:0)

在maping文件中只需更改生成器方法

 <generator class="identity"></generator>

像这样的本地人:

<generator class="native"/>