映射异常。没有坚持“实体”

时间:2011-02-22 15:16:12

标签: nhibernate fluent-nhibernate

Hy guys,

我在网上搜索过,但没有找到解决这个问题的方法......

这是我的实体......

    public class Pedido : IKeyed<int>
{
    public virtual int Id { get; private set; }
    public virtual string Assunto { get; set; }
    public virtual string Data { get; set; }
    public virtual Categoria Categoria{ get; set; }
    public virtual Modulo Modulo { get; set; }
    public virtual Pessoa Pessoa { get; set; }
    public virtual Site Site { get; set; }
    public virtual Situacao Situacao { get; set; }
    public virtual IList<Interacao> Interacao { get; set; }

    public Pedido()
    {
        Interacao = new List<Interacao>();
    }

    public virtual void addCategoria(Categoria categoria)
    {
        Categoria=categoria;
    }

    public virtual void addInteracao(Interacao interacao)
    {
        interacao.Pedido = this;
        Interacao.Add( interacao );
    }

}

classMap ...

    public class PedidoMap : ClassMap<Pedido>
{
    PedidoMap()
    {
        Table( "z1_pedido" );
        Id( x => x.Id );
        Map( x => x.Assunto );
        Map( x => x.Data );
        References( x => x.Categoria ).Column( "Id" );
        References( x => x.Modulo ).Column( "Id" );
        References( x => x.Pessoa ).Column( "Id" );
        References( x => x.Site ).Column( "Id" );
        References( x => x.Situacao ).Column( "Id" );
        HasMany( x => x.Interacao ).LazyLoad().Inverse().Cascade.All();

    }
}

我的存储库:

public class Repository<T> : NHibernateContext, IKeyedRepository<int, T> where T : class, IKeyed<int>
{
    private readonly ISession _session;

    public Repository(ISession session)
    {
        _session = session;
    }

    public bool Add(T entity)
    {
        _session.Save( entity ); "<-- Here's where the debug stops and display the error"
        return true;
    }

测试方法:

                UnitOfWork unitOfWork = new UnitOfWork( helper.SessionFactory );

            Repository<Pedido> repository = new Repository<Pedido>( unitOfWork.Session );

            Pedido pedido = CreatePedido( string.Format( "Pedido {0}", i + 1 ), 10 );
            repository.Add( pedido ); "<-- Call the Repository."

            unitOfWork.Commit();
        }

请大家......帮助我!

THKS。

1 个答案:

答案 0 :(得分:11)

您需要configure FluentNhibernate在PedidoMap类所在的程序集中查找映射。像这样:

        var fluentConfig = Fluently.Configure();
        fluentConfig.Mappings(m => 
            m.FluentMappings.AddFromAssembly(Assembly.GetExecutingAssembly()));
        var cfg = fluentConfig.BuildConfiguration();