【发布时间】:2010-12-01 12:15:12
【问题描述】:
当我尝试使用 NHibernate 的连接子类将对象添加到数据库时出现错误。这是我的代码:
Pessoa.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();
}
}
}
}
}
Pessoa.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>
错误:
TestCase '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 contrast com o tipo de destino”在葡萄牙语中的意思是“对象与目标类型不匹配”。我无法弄清楚我做错了什么。 完全相同代码在我在 Pessoa.hbm.xml 中添加第二个加入子类并映射到 PessoaJuridica.cs 之前工作,然后我把那个映射放在那里并开始出现此错误。
【问题讨论】:
标签: c# nhibernate