我在使用Nh保存的时候,在主键生成策略generator class="native",数据设置为int自增时能够保存数据成功。
1 <?xml version="1.0" encoding="utf-8"?> 2 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Permission.Domain" namespace="Permission.Domain.Entities"> 3 <class name="Roles" table="Roles"> 4 <id name="RoleID" column="RoleID"> 5 <generator class="uuid.hex"/> 6 </id> 7 <property name="Description"/> 8 </class> 9 </hibernate-mapping> 10 11 namespace Permission.Domain.Entities 12 { 13 public class Roles 14 { 15 public virtual int RoleID { get; set; } 16 public virtual string Description { get; set; } 17 } 18 } 19 20 namespace Permission.DAL 21 { 22 public class RolesDAL 23 { 24 protected ISession session { get; set; } 25 public RolesDAL(ISession Session) 26 { 27 session = Session; 28 } 29 public object CreateRole(Roles role) 30 { 31 return session.Save(role); 32 } 33 34 public Users GetRoleByID(int userID) 35 { 36 return session.Get<Users>(userID); 37 } 38 } 39 } 40 41 public class SessionManager 42 { 43 private ISessionFactory _sessionFactory; 44 45 public SessionManager() 46 { 47 _sessionFactory = GetSessionFactory(); 48 } 49 public ISessionFactory GetSessionFactory() 50 { 51 return (new Configuration()).Configure().BuildSessionFactory(); 52 } 53 54 public ISession GetSession() 55 { 56 return _sessionFactory.OpenSession(); 57 } 58 } 59 60 61 62 private void button2_Click(object sender, EventArgs e) 63 { 64 Roles role = new Roles { Description = "111" }; 65 RolesDAL roleDAL = new RolesDAL(session); 66 var o = roleDAL.CreateRole(role); 67 68 MessageBox.Show("Role添加成功!"); 69 }
1 <?xml version="1.0" encoding="utf-8" ?> 2 <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > 3 <session-factory name="Permission.Win"> 4 <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property> 5 <property name="connection.connection_string">Data Source=WEI-PC;Initial Catalog=Permission;Integrated Security=True</property> 6 <property name="show_sql">true</property> 7 <property name="dialect">NHibernate.Dialect.MsSql2008Dialect</property> 8 <mapping assembly ="Permission.Domain"/> 9 </session-factory> 10 11 </hibernate-configuration>
现在这个已经没有人用了吗??
不知道楼主解决没,我也是同样的问题,我想在代码里面给主键赋值,添加的数据库,无错,但也没添加进去
貌似3.3的配置文件不是这么写的,我不清楚是不是向下兼容的
加Transaction!
using(var trans = session.BeginTransaction())
{
//TODO: SAVE UPDATE DELETE
}