使用“ FluentNHibernate”时出现错误。 .xml中的类似文件可以正常工作,但是我想使用“ FluentNHibernate”要容易得多。知道我犯错了吗以及如何解决此问题?
复制例外
NHibernate.MappingException was unhandled by user code
Message=No persister for: TreningToWork.Models.Person
Source=NHibernate
StackTrace:
at NHibernate.Impl.SessionFactoryImpl.GetEntityPersister(String entityName)
at NHibernate.Impl.SessionImpl.GetEntityPersister(String entityName, Object obj)
at NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.FireSave(SaveOrUpdateEvent event)
at NHibernate.Impl.SessionImpl.Save(Object obj)
at NHibernateTutorial.PersonRepository.Add(Person newPerson) in D:\praca\TreningToWork\TreningToWork\Repository\PersonRepository.cs:line 15
at TreningToWork.Controllers.ShowDataController.Index() in D:\praca\TreningToWork\TreningToWork\Controllers\ShowDataController.cs:line 21
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
InnerException:
例外(更多详细信息):
例外(更多详细信息):
FluentNHibernate(与.xml相同)
using System.ComponentModel.DataAnnotations;
using FluentNHibernate.Automapping;
using FluentNHibernate.Automapping.Alterations;
namespace TreningToWork.Models
{
public class Person
{
[Key]
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Surname { get; set; }
}
public class PersonMappingAltertation : IAutoMappingOverride<Person>
{
public void Override(AutoMapping<Person> mapping)
{
mapping.Table("Person");
mapping.Id(x => x.Id, "Id").GeneratedBy.Identity();
mapping.Map(x=> x.Name, "Name").Column("Name").Not.Nullable();
mapping.Map(x => x.Surname, "Surname").Column("Surname").Not.Nullable();
}
}
}
人帮手
using NHibernate;
using NHibernate.Cfg;
using TreningToWork.Models;
namespace TreningToWork.ModelsHelper
{
public class PersonHelper
{
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
var configuration = new Configuration();
configuration.Configure();
configuration.AddAssembly(typeof(Person).Assembly);
_sessionFactory = configuration.BuildSessionFactory();
}
return _sessionFactory;
}
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
}
人资料库
using NHibernate;
using TreningToWork.Models;
using TreningToWork.ModelsHelper;
namespace NHibernateTutorial
{
public class PersonRepository
{
public void Add(Person newPerson)
{
using (ISession session = PersonHelper.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(newPerson);
transaction.Commit();
}
}
}
public Person GetPersonByName(string name)
{
using (ISession session = PersonHelper.OpenSession())
{
var result = session.QueryOver<Person>().Where(x => x.Name == name).SingleOrDefault();
return result ?? new Person();
}
}
public void Update(Person newPerson)
{
using (ISession session = PersonHelper.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Update(newPerson);
transaction.Commit();
}
}
}
public void Delete(Person newPerson)
{
using (ISession session = PersonHelper.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Delete(newPerson);
transaction.Commit();
}
}
}
}
}
编辑文件错误
System.ArgumentNullException was unhandled by user code
Message=Value cannot be null.
Parameter name: Data Source
Source=System.Data
ParamName=Data Source
StackTrace:
at System.Data.SqlClient.SqlConnectionStringBuilder.set_DataSource(String value)
at FluentNHibernate.Cfg.Db.MsSqlConnectionStringBuilder.Create()
at FluentNHibernate.Cfg.Db.PersistenceConfiguration`2.CreateProperties()
at FluentNHibernate.Cfg.Db.PersistenceConfiguration`2.ConfigureProperties(Configuration nhibernateConfig)
at FluentNHibernate.Cfg.FluentConfiguration.Database(IPersistenceConfigurer config)
at TreningToWork.ModelsHelper.PersonHelper.get_SessionFactory() in D:\praca\TreningToWork\TreningToWork\ModelsHelper\PersonHelper.cs:line 20
at TreningToWork.ModelsHelper.PersonHelper.OpenSession() in D:\praca\TreningToWork\TreningToWork\ModelsHelper\PersonHelper.cs:line 40
at NHibernateTutorial.PersonRepository.Add(Person newPerson) in D:\praca\TreningToWork\TreningToWork\Repository\PersonRepository.cs:line 11
at TreningToWork.Controllers.ShowDataController.Index() in D:\praca\TreningToWork\TreningToWork\Controllers\ShowDataController.cs:line 21
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
InnerException:
我的工厂变了
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using TreningToWork.Models;
namespace TreningToWork.ModelsHelper
{
public class PersonHelper
{
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(c => c
.FromAppSetting("rafalConnectionString"))
//<%$ ConnectionStrings:rafalConnectionString %>
.ShowSql())
.Mappings(m => m
.FluentMappings.AddFromAssemblyOf<Person>())
.BuildSessionFactory();
// var configuration = new Configuration();
// configuration.Configure();
// configuration.AddAssembly(typeof(Person).Assembly);
// _sessionFactory = configuration.BuildSessionFactory();
}
return _sessionFactory;
}
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
}
确实,我在配置时遇到了问题。我将项目添加到这篇文章中。告诉我我在哪里犯了错误,以及如何解决此问题。我没有找到好的例子,这个项目可能会在将来完成时转到youtube。对于其他用户。
如果您了解这篇文章中的所有文件,则不必上传我的项目。
重要文件:
控制器->有控制器
模型,ModelsHelper,存储库和视图。如果您打开网站,我的软件商必须将新表和数据放入数据库。现在我对此有疑问。当我使用.xml文件时,一切正常,但是我想使用“ FluentNHibernate”,这是一个问题。
文件:My project download praca.zip
下一个错误:
System.ArgumentNullException was unhandled by user code
Message=Value cannot be null.
Parameter name: Data Source
Source=System.Data
ParamName=Data Source
StackTrace:
at System.Data.SqlClient.SqlConnectionStringBuilder.set_DataSource(String value)
at FluentNHibernate.Cfg.Db.MsSqlConnectionStringBuilder.Create()
at FluentNHibernate.Cfg.Db.PersistenceConfiguration`2.CreateProperties()
at FluentNHibernate.Cfg.Db.PersistenceConfiguration`2.ConfigureProperties(Configuration nhibernateConfig)
at FluentNHibernate.Cfg.FluentConfiguration.Database(IPersistenceConfigurer config)
at TreningToWork.ModelsHelper.PersonHelper.get_SessionFactory() in D:\praca\TreningToWork\TreningToWork\ModelsHelper\PersonHelper.cs:line 21
at TreningToWork.ModelsHelper.PersonHelper.OpenSession() in D:\praca\TreningToWork\TreningToWork\ModelsHelper\PersonHelper.cs:line 50
at NHibernateTutorial.PersonRepository.Add(Person newPerson) in D:\praca\TreningToWork\TreningToWork\Repository\PersonRepository.cs:line 11
at TreningToWork.Controllers.ShowDataController.Index() in D:\praca\TreningToWork\TreningToWork\Controllers\ShowDataController.cs:line 21
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
InnerException:
最佳答案
您的项目存在的问题太多了,无法列出。我进行了许多更改(主要是删除了不必要的代码),使其生效并将其上传到http://www.sendspace.pl/en/file/8f7f2ebf8fabf912800d64c。
关键的变化是使用Ninject依赖项注入框架将会话工厂创建为单例,并根据请求创建会话。
如果您有任何疑问,请告诉我。
关于c# - 使用FluentNHibernate NHibernate 3.3错误的映射,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15087904/