asp.net-mvc – Sql Views的流畅Nhibernate映射

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – Sql Views的流畅Nhibernate映射前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用Fluent Nhibernate在asp.net mvc3与c#

我正在按照以下方式生成和映射一个类

制图

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);
              }
          }
       }

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}

           }

        }

配置文件

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();


    }

现在问题来了,我在sql中创建一个视图看起来像这样

sql视图

CREATE VIEW [FeaturedCompanies] AS

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

我想在我的代码中使用这个视图,就像我正在使用,但我怎么能这样做,我搜索了很多,但在谷歌没有发现

请帮助我,提前感谢

到目前为止已经尝试了什么

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

制图

public class FeaturedCompanyMap : ClassMap<FeaturedCompany>
  {
 public FeaturedCompanyMap()
 {
    Table("FeaturedCompanies");
    ReadOnly();
   Id(x => x.id);
   Map(x => x.name);
   Map(x => x.count);
 }
}

解决方法

视图的映射方式与表映射的方式相同,只是您应该将Readonly()放在映射中,以防止意外写入.例:
public class FeaturedCompanyMap : ClassMap<FeaturedCompany>
{
    public FeaturedCompanyMap ()
    {
       Table("FeaturedCompanies");
       ReadOnly();

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

更新:得到计数

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
}

猜你在找的asp.Net相关文章