Click here to Skip to main content
15,885,214 members
Articles / Database Development

Fluent NHibernate - Working with Database Views

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
5 Mar 2011CPOL 32.8K   1   6
Fluent NHibernate - Working with Database Views

So, it turns out you can work with Views with Fluent NHibernate just as you were to work with tables. All you need to do is define your entity with the name of the view, instead of the table name.

For example:

C#
public class UserEntity
{
    public virtual int UserId { get; private set; }
    public virtual String FirstName { get; set; }
    public virtual String LastName { get; set; }
    public virtual int UserStatusId { get; set; }
    public virtual String UserStatus { get; set; }
}

public class UserEntityMap : ClassMap<UserEntity>
{
    public UserEntityMap()
    {
        Table("view_Users");  // this is mapped to a view, and not a table

        Id(x => x.UserId);
        Map(x => x.FirstName);
        Map(x => x.LastName);
        Map(x => x.UserStatusId);
        Map(x => x.UserStatus);  // This field is from another table
                                 // it is from a separate code table 
			// that describes the different statuses in the system
    }
}

An exception is thrown, when trying to update the entity that is mapped to a view. The problem is actually because when working with a view, you cannot execute an update query that updates rows on different tables. It will only work when updating rows on one table in the view.

In order to get around this, we need to tell the mapping that some properties aren't to be updated. This will solve the problem.

For example:

C#
public class UserEntityMap : ClassMap<UserEntity>
{
    public UserEntityMap()
    {
        Table("view_Users");  // this is mapped to a view, and not a table

        Id(x => x.UserId);
        Map(x => x.FirstName);
        Map(x => x.LastName);
        Map(x => x.UserStatusId);
        Map(x => x.UserStatus).Not.Update();
    }
}

Marking the mapping class with '.Not.Update()' tells FNH to return false on the update property of this field.

Likewise, we can also mark an attribute as '.Not.Insert()' and then the field will only be updatable, or mark a field as '.ReadOnly()' and the field will act as if it has a private set.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Israel Israel
Started programming e-commerce sites with PHP & MySQL at the age of 14. Worked for me well for about 5 years.

Transfered to C# & asp.net, while serving in the IDF.
Worked on the 'Core Performance' Team at ShopYourWay.com (Sears Israel)
Currently working at Logz.io

Check out my blog!
or my twitter

Comments and Discussions

 
QuestionRestrict Update Pin
aldari17-Jan-13 20:30
aldari17-Jan-13 20:30 
AnswerRe: Restrict Update Pin
Gilly Barr2-Feb-13 7:08
Gilly Barr2-Feb-13 7:08 
QuestionHow to refresh view values Pin
Metalp27-Jun-12 8:24
Metalp27-Jun-12 8:24 
Generalmapping doesn't work for me Pin
abrzezin23-Mar-11 6:02
abrzezin23-Mar-11 6:02 
GeneralRe: mapping doesn't work for me Pin
abrzezin23-Mar-11 7:14
abrzezin23-Mar-11 7:14 
GeneralRe: mapping doesn't work for me Pin
Gilly Barr23-Mar-11 22:03
Gilly Barr23-Mar-11 22:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.