gpt4 book ai didi

c# - NHibernate 加入子类错误 - 对象与目标类型不匹配

转载 作者:太空宇宙 更新时间:2023-11-03 18:00:54 24 4
gpt4 key购买 nike

当我尝试使用 NHibernate 的连接子类将对象添加到数据库时出现错误。这是我的代码:

佩索阿.cs

namespace CarvalhoRodrigues.Domain.Cadastro
{
public class Pessoa
{
public Pessoa()
{
this.Endereco = new List<Endereco>();
}

public virtual long Id { get; set; }
public enum TipoPessoa { Fisica, Juridica }
public virtual TipoPessoa Tipo { get; set; }
public virtual ICollection<Endereco> Endereco { get; set; }

}
}

PessoaFisica.cs
namespace CarvalhoRodrigues.Domain.Cadastro
{
public class PessoaFisica : Pessoa
{
public virtual string CPF { get; set; }
public virtual string Nome { get; set; }
public virtual DateTime DataNascimento { get; set; }
}
}

PessoaJuridica.cs
namespace CarvalhoRodrigues.Domain.Cadastro
{
public class PessoaJuridica
{
public virtual string CNPJ { get; set; }
public virtual string RazaoSocial { get; set; }
public virtual DateTime DataConstituicao { get; set; }
public virtual string NomeFantasia { get; set; }
public virtual ICollection<Pessoa> Representantes { get; set; }
}
}

PessoaRepository.cs
namespace CarvalhoRodrigues.Domain.Repositories.Cadastro
{
class PessoaRepository : IPessoaRepository
{
public void Inserir(Pessoa Pessoa)
{
using (ISession session = NHibernateHelper.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
session.Save(Pessoa);
transaction.Commit();
}
}
}
}
}

佩索阿.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="CarvalhoRodrigues.Domain"
namespace="CarvalhoRodrigues.Domain.Cadastro">

<class name="Pessoa">
<id name="Id">
<generator class="identity" />
</id>
<property name="Tipo" />

<bag name="Endereco" lazy="false">
<key column="PessoaId" />
<one-to-many class="Endereco" />
</bag>

<joined-subclass name="PessoaFisica">
<key column="PessoaId" />
<property name="CPF" />
<property name="Nome" />
<property name="DataNascimento" />
</joined-subclass>

<joined-subclass name="PessoaJuridica">
<key column="PessoaId" />
<property name="CNPJ" />
<property name="RazaoSocial" />
<property name="DataConstituicao" />
<property name="NomeFantasia" />
</joined-subclass>

</class>

</hibernate-mapping>

错误:

测试用例'CarvalhoRodrigues.Tests.GenerateSchema_Fixture.Can_add_pessoa'
失败:NHibernate.PropertyAccessException:CarvalhoRodrigues.Domain.Cadastro.Pessoa.Id 发生异常
----> System.Reflection.TargetException : Objeto não 与 com o tipo de destino 相吻合。
em NHibernate.Properties.BasicPropertyAccessor.BasicGetter.Get(对象目标)
em NHibernate.Engine.UnsavedValueFactory.GetUnsavedIdentifierValue(String unsavedValue, IGetter identifierGetter, IType identifierType, ConstructorInfo 构造函数)
em NHibernate.Tuple.PropertyFactory.BuildIdentifierProperty(PersistentClass mappedEntity,IIdentifierGenerator 生成器)
em NHibernate.Tuple.Entity.EntityMetamodel..ctor(PersistentClass persistentClass, ISessionFactoryImplementor sessionFactory)
em NHibernate.Persister.Entity.AbstractEntityPersister..ctor(PersistentClass persistentClass,ICacheConcurrencyStrategy 缓存,ISessionFactoryImplementor 工厂)
em NHibernate.Persister.Entity.JoinedSubclassEntityPersister..ctor(PersistentClass persistentClass, ICacheConcurrencyStrategy cache, ISessionFactoryImplementor factory, IMapping mapping)
em NHibernate.Persister.PersisterFactory.CreateClassPersister(PersistentClass 模型,ICacheConcurrencyStrategy 缓存,ISessionFactoryImplementor 工厂,IMapping cfg)
em NHibernate.Impl.SessionFactoryImpl..ctor(Configuration cfg, IMapping mapping, Settings settings, EventListeners listeners)
em NHibernate.Cfg.Configuration.BuildSessionFactory()
D:\Projetos\CarvalhoRodrigues\CarvalhoRodrigues.Domain\NhibernateHelper.cs(19,0): em CarvalhoRodrigues.Domain.NHibernateHelper.get_SessionFactory()
D:\Projetos\CarvalhoRodrigues\CarvalhoRodrigues.Domain\NhibernateHelper.cs(27,0): em CarvalhoRodrigues.Domain.NHibernateHelper.OpenSession()
D:\Projetos\CarvalhoRodrigues\CarvalhoRodrigues.Domain\Repositories\Cadastro\PessoaRepository.cs(35,0): em CarvalhoRodrigues.Domain.Repositories.Cadastro.PessoaRepository.Inserir(Pessoa Pessoa)
D:\Projetos\CarvalhoRodrigues\CarvalhoRodrigues.Domain\Tests\PessoaTests.cs(47,0): em CarvalhoRodrigues.Tests.GenerateSchema_Fixture.Can_add_pessoa()
--目标异常
em System.Reflection.RuntimeMethodInfo.CheckConsistency(对象目标)
em System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] 参数, CultureInfo 文化, Boolean skipVisibilityChecks)
em System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] 参数, CultureInfo 文化)
em System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
em NHibernate.Properties.BasicPropertyAccessor.BasicGetter.Get(对象目标)

“Objeto não 一致 com o Tipo de destino”在葡萄牙语中的意思是“对象与目标类型不匹配”。我不知道我做错了什么。 完全相同在我在 Pessoa.hbm.xml 中添加第二个加入子类并映射到 PessoaJuridica.cs 之前,代码正在工作,然后我把那个映射放在那里并开始收到这个错误。

最佳答案

我可能会在这里离开,但 PessoaJuridica 不应该来自 Pessoa 或 PessoaFisica 吗?

关于c# - NHibernate 加入子类错误 - 对象与目标类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1478687/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com