Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fluent Nhibernate Mapping for Sql Views

i am using Fluent Nhibernate in asp.net mvc3 with c#

i am working in following way to generate and map a class

Mapping

using FluentNHibernate.Mapping;
using Com.Web.Domain;

   namespace Com.Web.Mapping
      {
         public class CompanyMap : ClassMap<Company>
        {
             public CompanyMap()
            {
               Id(x => x.id);
               Map(x => x.Name);
              }
          }
       }

Class

     using System.Collections.Generic;
     using System;

      namespace Com.Web.Domain
      {

          public class Company
         {

          public virtual int id { get; set; }
          public virtual string Name{get;set}

           }

        }

and in configuration file

  private static void InitializeSessionFactory()
    {

       _sessionFactory = Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008
                          .ConnectionString(local)

            )
            .Mappings(m =>
                      m.FluentMappings
                          .AddFromAssemblyOf<Company>())
            .ExposeConfiguration(cfg => new SchemaExport(cfg)
            .Create(false, false))  // this is intentionally set false , bcz i dont want to regenerate table when application starts every time 
            .BuildSessionFactory();


    }

now problem come i create a view in sql looks like this

Sql View

CREATE VIEW [FeaturedCompanies] AS

   SELECT COUNT(Company.id) As Count FROM Company
   WHERE Name='Alias'

i want to use this view in my code as a like i am using but how can i do that , i searched alot but found nothing on google

Pleas help me out and thanks in advance

what is tried so far

Class

public class FeaturedCompany
{
    public virtual int id { get; set; }
    public virtual int name { get; set; }
    public virtual int count { get; set; }
}

Mapping

public class FeaturedCompanyMap : ClassMap<FeaturedCompany>
  {
 public FeaturedCompanyMap()
 {
    Table("FeaturedCompanies");
    ReadOnly();
   Id(x => x.id);
   Map(x => x.name);
   Map(x => x.count);
 }
}
like image 774
smart boy Avatar asked Oct 05 '12 08:10

smart boy


People also ask

What is fetch in NHibernate?

A fetching strategy is the strategy NHibernate will use for retrieving associated objects if the application needs to navigate the association. Fetch strategies may be declared in the O/R mapping metadata, or overridden by a particular HQL or Criteria query.

What is NHibernate in asp net?

NHibernate is an actively developed, fully featured, open source object-relational mapper for the . NET framework. It is used in thousands of successful projects. It's built on top of ADO.NET and the current version is NHibernate 4.0.


1 Answers

Views are mapped the same way tables are mapped except that you should put Readonly() in the mapping to prevent accidently writing to it. Example:

public class FeaturedCompanyMap : ClassMap<FeaturedCompany>
{
    public FeaturedCompanyMap ()
    {
       Table("FeaturedCompanies");
       ReadOnly();

       Id(x => x.Id);
       Map(x => x.Name);
       Map(x => x.Count);
    }
}

Update: to get the counts

var results = session.Query<FeaturedCompany>().Where(filter).List();

foreach(var row in results.Select(r => "Alias: " + r.Name + "  Occurence: " + r.Count))
{
    // print row to screen
}
like image 160
Firo Avatar answered Sep 18 '22 12:09

Firo