FluentNHibernate配置:
private static ISessionFactory sessionFactory { get; set; }
public static ISessionFactory GetCurrectFactory()
{
if (sessionFactory == null)
{
sessionFactory = GetSessionFactory();
}
return sessionFactory;
}
private static ISessionFactory GetSessionFactory()
{
return FluentNHibernate.Cfg.Fluently.Configure()
.Database(FluentNHibernate.Cfg.Db.MsSqlConfiguration.MsSql2008
.ConnectionString(s => s.Server(".")
.Database("PersonInfo")
.Username("test")
.Password("test")))
.Mappings(o => o.FluentMappings
.AddFromAssembly(typeof(User).Assembly)).BuildSessionFactory();
}
}
映射:
public class UserMap:ClassMap<User>
{
public UserMap()
{
Table("User");
Id(m => m.Id).GeneratedBy.Native().Column("id");
Map(m => m.UserName).Column("UserName");
Map(m => m.Age).Column("Age");
Map(m => m.Address).Column("Address");
Map(m => m.Phone).Column("Phone");
LazyLoad();
}
}
实体类:
public class User
{
public virtual int Id { get; set; }
public virtual string UserName { get; set; }
public virtual int Age { get; set; }
public virtual string Address { get; set; }
public virtual string Phone { get; set; }
}
Controll代码:
public ActionResult AddUser(UserModel model)
{
if (ModelState.IsValid)
{
using (ISession session = SessionFactory.GetCurrectFactory().OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
Entities.User user = new User();
user.UserName = model.UserName;
user.Age = model.Age;
user.Address = model.Address;
user.Phone = model.Phone;
session.Save(user);
session.Flush();
transaction.Commit();
}
}
return View();
}
return View();
}
是配置出问题了吗?