功能NHibernate映射的SQL视图 [英] Fluent Nhibernate Mapping for Sql Views
问题描述
我使用功能NHibernate在asp.net MVC3用C#
我在下面的工作方式产生并映射一类
映射
使用FluentNHibernate.Mapping;
使用Com.Web.Domain; 命名空间Com.Web.Mapping
{
公共类CompanyMap:ClassMap<公司+ GT;
{
公共CompanyMap()
{
ID(X => x.id);
地图(X => x.Name);
}
}
}
类
使用System.Collections.Generic;
使用系统; 命名空间Com.Web.Domain
{ 公共类公司
{ 公共虚拟INT编号{搞定;组; }
公共虚拟字符串名称{;}集 } }
和配置文件
私有静态无效InitializeSessionFactory()
{ _sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008
.ConnectionString(本地) )
.Mappings(M =>
m.FluentMappings
.AddFromAssemblyOf<公司+ GT;())
.ExposeConfiguration(CFG =>新建的SchemaExport(CFG)
.Create(假,假))//这是故意设置为false,BCZ我不想再生表时,应用程序启动每次
.BuildSessionFactory();
}
现在问题来了我在SQL创建一个视图看起来像这样
SQL视图
CREATE VIEW [FeaturedCompanies] AS SELECT COUNT(Company.id)作为计数值从公司
其中name ='别名'
我想用这个观点在我的code作为一个像我使用的,但我怎么能做到这一点,我搜索了很多,但发现对谷歌没有什么
普莱斯帮我感谢事先
到目前为止什么试过
类
公共类FeaturedCompany
{
公共虚拟INT编号{搞定;组; }
公共虚拟INT名称{;组; }
公共虚拟诠释计数{搞定;组; }
}
映射
公共类FeaturedCompanyMap:ClassMap< FeaturedCompany>
{
公共FeaturedCompanyMap()
{
表(FeaturedCompanies);
只读();
ID(X => x.id);
地图(X => x.name);
地图(X => x.count);
}
}
视图映射表映射除了你应该把只读()
在映射以同样的方式以prevent无意中写它。例如:
公共类FeaturedCompanyMap:ClassMap< FeaturedCompany>
{
公共FeaturedCompanyMap()
{
表(FeaturedCompanies);
只读(); ID(X => x.Id);
地图(X => x.Name);
地图(X => x.Count);
}
}
更新:获得计数
VAR的结果= session.Query< FeaturedCompany方式>()式(过滤器)的.List();的foreach(在results.Select VAR行(R = GT;别名:+ r.Name +Occurence:+ r.Count))
{
//打印行筛选
}
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);
}
}
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
}
这篇关于功能NHibernate映射的SQL视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!