Nhibernate:发生异常 ID 获取器

本文关键字:ID 获取 异常 Nhibernate | 更新日期: 2023-09-27 17:55:56

嗨,我得到了解释:

NHibernate.PropertyAccessException 未由用户代码
处理 HResult=-2146232832 消息=异常发生 getter of SpellenDatabase.Domain.Model.Location.Id 来源=NHibernate
堆栈跟踪: bij NHibernate.Properties.BasicPropertyAccessor.BasicGetter.Get(Object 目标) bij NHibernate.Tuple.Entity.AbstractEntityTuplizer.GetIdentifier(Object 实体) bij NHibernate.Persister.Entity.AbstractEntityPersister.GetIdentifier(Object obj, 实体模式 实体模式) bij NHibernate.Persister.Entity.AbstractEntityPersister.IsTransient(Object 实体,ISessionImplementor session) bij NHibernate.Engine.ForeignKeys.IsTransient(String entityName, Object entity, Nullable 1 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:''Users''Gebruiker''Documents''Programmeren''SpellenDatabase''SpellenDatabase.DataLayer''BaseRepository.cs:regel 27 bij SpellenDatabase.Web.Controllers.ActiviteitController.Create(Activiteit activiteit) in C:''Users''Gebruiker''Documents''Programmeren''SpellenDatabase''SpellenDatabase.Web''Controllers''ActiviteitController.cs:regel 81 bij lambda_method(闭包 , 控制器库 , 对象[] ) bij System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase 控制器,对象[] 参数) bij System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary 2 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 Message=Het object komt niet overeen met het doeltype. 来源=mscorlib 堆栈跟踪: bij System.Reflection.RuntimeMethodInfo.CheckConsistency(Object target) bij System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, 文化信息文化) bij System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, 文化信息文化) 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 目标) 内部异常:

在使用位置用法插入活动并引用现有位置时,我遇到了异常。 当我插入一个新位置时,我没有异常。

我希望有人1可以帮助我解决这个问题,我找不到任何有帮助的东西

我的班级:

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

Nhibernate:发生异常 ID 获取器

你一对一做错了。

  <class name="Location" table="Location">
    <id name="Id" type="Int32">
      <column name="Id" sql-type="int" />
      <generator class="increment" />
    </id>

该位置以一对一的方式映射到活动。 这意味着,它共享相同的主键。所以他们不能都拥有自己的发电机。

看看Ayende的这篇很棒的帖子。

老实说,我不会使用一对一。实在是太乱了。使用常规的多对一(这会将另一个外键添加到 Activiteit 表中)。