0

这里我使用了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 应该也是自动分配的啊

4

1 回答 1

0

在您的映射文件中,只需更改您的生成器方法

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

像这样的本地人:

<generator class="native"/>
于 2016-07-25T20:49:58.700 回答