gpt4 book ai didi

asp.net-mvc - Sql View 的 Fluent Nhibernate 映射

转载 作者:行者123 更新时间:2023-12-03 16:14:17 24 4
gpt4 key购买 nike

我正在使用 C# 在 asp.net mvc3 中使用 Fluent Nhibernate

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

制图

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}

}

}

并在配置文件中
  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 中创建了一个 View ,如下所示

SQL View
CREATE VIEW [FeaturedCompanies] AS

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

我想在我的代码中使用这个 View ,就像我正在使用的那样,但我该怎么做,我搜索了很多但在谷歌上什么也没找到

请帮助我,并提前致谢

到目前为止尝试了什么

类(class)
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);
}
}

最佳答案

View 的映射方式与表的映射方式相同,不同之处在于您应该放置 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-mvc - Sql View 的 Fluent Nhibernate 映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12742651/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com