user1169684
user1169684

Reputation: 21

Using fluent NHibernate to map a read only sql view

I am trying to get Fluent Nhibernate to Automap read-only views that do not have a primary or composite key specified. It seems like it is possible as the API document for ClassMap exposes a default constructor for Id().

http://fluentnhibernate.org/api/FluentNHibernate.Mapping/ClassMap%601.htm

it says

IdentityPart Id() Create an Id that doesn't have a corresponding property in the domain object, or a column in the database. This is mainly for use with read-only access and/or views. Defaults to an int identity with an "increment" generator.

I have tried to override the mapping for the entity which corresponds to my view, and have removed the primary key convention that specifies the Id field name. It adds an id column to queries on the view which fail because there is no such column.

I am trying to get rid of fake primary keys on my views as I think there should be a better way to do this.

Here is the override snippet I am using

public class PersonMap: IAutoMappingOverride<Person>
{
    public void Override(AutoMapping<Person> mapping)
    {
        mapping.Id();
    }
}

Why doesn't this work?

Upvotes: 2

Views: 2946

Answers (1)

Fourth
Fourth

Reputation: 9351

The way around this is to typically assign an arbitrary property to be the id and its generated by assigned. This is essentially the same as doing Map.

public class ItemViewMap : IAutoMappingOverride<ItemView>
{
    public void Override(AutoMapping<ItemView> mapping)
    {
        mapping.Id(x => x.Column).GeneratedBy.Assigned();
    }
}

Upvotes: 1

Related Questions