首页 新闻 会员 周边 捐助

FluentNHibernate+mvc 保存数据是出现异常:" No persister for:Entity.User " 请问各位大侠哪出问题了?

0
悬赏园豆:50 [已关闭问题] 解决于 2013-09-15 16:38

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();
        }

问题补充:

是配置出问题了吗?

w_xin的主页 w_xin | 初学一级 | 园豆:139
提问于:2013-09-11 22:25
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册