我在MVC中使用Fluent NHibernate和JsonResult来从SQL Server获取我的查询。当我从本地测试时,我的查询工作正常,但是当我想从IIS或网络测试时,我收到此错误:
异常详细信息:System.NullReferenceException:未将对象引用设置为对象的实例。
我的代码是:错误在代码
下面 public JsonResult getDataType()
{
using (var session = NHibernate.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
var query = session.CreateSQLQuery("select Name from DataType");
var result = query.List();
return Json(result, JsonRequestBehavior.AllowGet);
}
}
}
我的NHibernate类是:
public class NHibernate
{
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
InitializeSessionFactory();
return _sessionFactory;
}
}
public static void InitializeSessionFactory()
{
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(
@"Server=.;initial catalog=Mahsa;;User ID=;Password=")
.ShowSql()
)
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<GenerateDBController>())
.ExposeConfiguration(cfg => new SchemaUpdate(cfg)
.Execute(false, true))
.BuildSessionFactory();
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
虽然此代码在本地或IIS中正常工作
public ActionResult Mahsa()
{
using (var session = NHibernate.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
var DataTypeObject = new DataType { Name = "IT" };
session.Save(DataTypeObject);
transaction.Commit();
}
}
return View();
}
先谢谢你的帮助。