Search code examples
c#nhibernatefluent-nhibernateautomapping

Mapping db view without Id to class model in NHibernate


In my application, I use NHibernate ORM and Automapper to mapping entities to class model. As: fluent-nhibernate/wiki/Auto-mapping

For tables its working.

Problem is when a try mapping db view without Id field, like:

public class VTest
{

    [NotNull]
    public virtual AAATab AAA { get; set; }
    [NotNull]
    public virtual BBBTab BBB { get; set; }

}

I create composite key :

public void Override(AutoMapping<VTest> mapping)
    {
        mapping.IgnoreProperty(x => x.Id);
        mapping.CompositeId()
                .KeyProperty(x => x.AAA.Id)
               .KeyProperty(x => x.BBB.Id);
    }

but it not working. A get error, becouse in the db query have select id :

[GenericADOException: could not execute query [ SELECT this_.Id as Id7_0_, this_.AAAId as AAAId7_0_, this_.BBBId as BBB_7_0_ FROM [VTest] this_ ]

Its possible use automapper for this case?


Solution

  • I find solutions. In compositeKey must use phisical exist property in db view, so i create new properties:

    public virtual int AAAId {get;set;}
    public virtual int BBBId {get;set;}
    

    And i create:

        mapping.IgnoreProperty(x => x.Id);
        mapping.CompositeId()
               .KeyProperty(x => x.AAAId)
               .KeyProperty(x => x.BBBId);
    

    This automapping for view or table without Id works. (To model dbview or table, you need add override function Equals() and GetHashCode() )