嗨我得到了这个例子:
NHibernate.PropertyAccessException未被用户代码
处理 HResult = -2146232832消息=异常发生的getter SpellenDatabase.Domain.Model.Location.Id Source = NHibernate
堆栈跟踪: bij NHibernate.Properties.BasicPropertyAccessor.BasicGetter.Get(Object 目标) bij NHibernate.Tuple.Entity.AbstractEntityTuplizer.GetIdentifier(Object 实体) bij NHibernate.Persister.Entity.AbstractEntityPersister.GetIdentifier(Object obj,EntityMode entityMode) bij NHibernate.Persister.Entity.AbstractEntityPersister.IsTransient(Object 实体,ISessionImplementor会话) bij NHibernate.Engine.ForeignKeys.IsTransient(String entityName,Object entity,Nullable1 assumed, ISessionImplementor session) bij NHibernate.Event.Default.AbstractSaveEventListener.GetEntityState(Object entity, String entityName, EntityEntry entry, ISessionImplementor source) bij NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event) bij NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event) bij NHibernate.Impl.SessionImpl.FireSaveOrUpdate(SaveOrUpdateEvent event) bij NHibernate.Impl.SessionImpl.SaveOrUpdate(String entityName, Object obj) bij NHibernate.Engine.CascadingAction.SaveUpdateCascadingAction.Cascade(IEventSource session, Object child, String entityName, Object anything, Boolean isCascadeDeleteEnabled) bij NHibernate.Engine.Cascade.CascadeToOne(Object parent, Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled) bij NHibernate.Engine.Cascade.CascadeAssociation(Object parent, Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled) bij NHibernate.Engine.Cascade.CascadeProperty(Object parent, Object child, IType type, CascadeStyle style, Object anything, Boolean isCascadeDeleteEnabled) bij NHibernate.Engine.Cascade.CascadeOn(IEntityPersister persister, Object parent, Object anything) bij NHibernate.Event.Default.AbstractSaveEventListener.CascadeAfterSave(IEventSource source, IEntityPersister persister, Object entity, Object anything) bij NHibernate.Event.Default.AbstractSaveEventListener.PerformSaveOrReplicate(Object entity, EntityKey key, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess) bij NHibernate.Event.Default.AbstractSaveEventListener.PerformSave(Object entity, Object id, IEntityPersister persister, Boolean useIdentityColumn, Object anything, IEventSource source, Boolean requiresImmediateIdAccess) bij NHibernate.Event.Default.AbstractSaveEventListener.SaveWithGeneratedId(Object entity, String entityName, Object anything, IEventSource source, Boolean requiresImmediateIdAccess) bij NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.SaveWithGeneratedOrRequestedId(SaveOrUpdateEvent event) bij NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.EntityIsTransient(SaveOrUpdateEvent event) bij NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.PerformSaveOrUpdate(SaveOrUpdateEvent event) bij NHibernate.Event.Default.DefaultSaveOrUpdateEventListener.OnSaveOrUpdate(SaveOrUpdateEvent event) bij NHibernate.Impl.SessionImpl.FireSaveOrUpdate(SaveOrUpdateEvent event) bij NHibernate.Impl.SessionImpl.SaveOrUpdate(Object obj) bij SpellenDatabase.DataLayer.BaseRepository
1.SaveOrUpdate(T item)in C:\用户\ Gebruiker \文件\ Programmeren \ SpellenDatabase \ SpellenDatabase.DataLayer \ BaseRepository.cs:葱 27 bij SpellenDatabase.Web.Controllers.ActiviteitController.Create(Activiteit 活动) C:\用户\ Gebruiker \文档\ Programmeren \ SpellenDatabase \ SpellenDatabase.Web \ \控制器ActiviteitController.cs:葱 81 bij lambda_method(Closure,ControllerBase,Object []) bij System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller,Object []参数) bij System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext,IDictionary2 parameters) bij System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 参数) bij System.Web.Mvc.ControllerActionInvoker。<> c__DisplayClass15.b__12() bij System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter) filter,ActionExecutingContext preContext,Func`1 continuation)
InnerException:System.Reflection.TargetException 的HResult = -2146232829 消息= Het对象komt niet overeen遇到了het doeltype。 来源= mscorlib程序 堆栈跟踪: bij System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target) bij System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr,Binder binder,Object []参数, 文化信息文化) bij System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder binder,Object []参数, 文化信息文化) bij System.Reflection.RuntimePropertyInfo.GetValue(Object obj,BindingFlags invokeAttr,Binder binder,Object [] index, 文化信息文化) bij System.Reflection.RuntimePropertyInfo.GetValue(Object obj,Object [] index) bij NHibernate.Properties.BasicPropertyAccessor.BasicGetter.Get(Object 目标) 的InnerException:
在使用Locationusage插入activiteit并引用现有位置时出现异常。当我插入一个新的位置时,我也没有例外。
我希望some1可以帮助我,我找不到任何有用的东西
我的班级:
public class Location
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IEnumerable<LocationUsage> Usage { get; set; }
}
public class LocationUsage
{
public virtual int Id { get; set; }
public virtual Location Location { get; set; }
public virtual Activiteit Activiteit { get; set; }
public virtual string Discription { get; set; }
}
public class Activiteit
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Discription { get; set; }
public virtual ICollection<Variatie> Variaties { get; set; }
public virtual int MinAge { get; set; }
public virtual int MaxAge { get; set; }
public virtual ActiviteitSoort ActiviteitSoort { get; set; }
public virtual LocationUsage Location { get; set; }
public virtual ICollection<MateriaalUseage> Materials { get; set; }
}
我的映射文件:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="SpellenDatabase.Domain.Model" assembly="SpellenDatabase.Domain">
<class name="Location" table="Location">
<id name="Id" type="Int32">
<column name="Id" sql-type="int" />
<generator class="increment" />
</id>
<property name="Name">
<column name="Name" not-null="true" />
</property>
<set name="Usage" inverse="true">
<key column="Id" />
<one-to-many class="LocationUsage" />
</set>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="SpellenDatabase.Domain.Model" assembly="SpellenDatabase.Domain">
<class name="LocationUsage" table="LocationUsage">
<id name="Id" type="Int32">
<column name="Id" sql-type="int" />
<generator class="increment" />
</id>
<many-to-one name="Location" class="Location" column="Location" cascade="none" />
<many-to-one name="Activiteit" class="Activiteit" column="Activiteit" unique="true" />
<property name="Discription" />
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" namespace="SpellenDatabase.Domain.Model" assembly="SpellenDatabase.Domain">
<class name="Activiteit" table="Activiteit">
<id name="Id">
<column name="Id" sql-type="int" not-null="true" />
<generator class="increment" />
</id>
<property name="Name">
<column name="Name" not-null="true" />
</property>
<property name="Discription">
<column name="Discription" not-null="true" />
</property>
<property name="MinAge" />
<property name="MaxAge" />
<one-to-one name="Location" class="Location" cascade="save-update" />
<many-to-one name="ActiviteitSoort" class="ActiviteitSoort" column="ActiviteitSoort" />
<set name="Variaties" cascade="save-update">
<key column="Id" />
<one-to-many class="Variatie" />
</set>
<set name="Materials" cascade="save-update">
<key column="Id" />
<one-to-many class="MateriaalUseage" />
</set>
</class>
</hibernate-mapping>
表:
create table Activiteit(
Id int,
Name varchar(50),
Discription varchar(max),
MinAge int,
MaxAge int,
ActiviteitSoort int,
PRIMARY KEY (Id),
FOREIGN KEY (ActiviteitSoort) REFERENCES ActiviteitSoort(Id))
create table Location (
Id int,
Name varchar(50),
PRIMARY KEY (Id))
create table LocationUsage(
Id int,
Location int,
Activiteit int,
Discription varchar(max),
PRIMARY KEY (Id),
FOREIGN KEY (Activiteit) REFERENCES Activiteit(Id),
FOREIGN KEY (Location) REFERENCES Location(Id))
C#:
ISession session = NHibernateHelper.GetCurrentSession();
ITransaction tx = session.BeginTransaction();
try
{
session.SaveOrUpdate(item);
session.Flush();
tx.Commit();
}
catch (Exception e)
{
tx.Rollback();
throw;
}
finally
{
NHibernateHelper.CloseSession();
}
答案 0 :(得分:0)
你做了一对一的错误。
<class name="Location" table="Location">
<id name="Id" type="Int32">
<column name="Id" sql-type="int" />
<generator class="increment" />
</id>
该位置被映射为Activiteit一对一。这意味着,它共享相同的主键。所以他们不能都有自己的发电机。
说实话,我不会一对一使用。这太乱了。使用常规的多对一(将另一个外键添加到Activiteit表中)。