c# – 使用实体框架代码中的SQL视图第一版本5

栏目: ASP.NET · 发布时间: 7年前

内容简介:翻译自:https://stackoverflow.com/questions/18595589/using-an-sql-view-from-an-entity-framework-code-first-version-5

我正在使用VS 2010,MVC3和EF 5在网站上开发联系人日志 – 首先使用代码创建实体.数据存储在SQL Server 2008 R2数据库集中.我想显示联系人日志的摘要并创建了一个视图.

CREATE VIEW dbo.ContactLogSummaries

AS

SELECT
    CLE.ContactLogEntryID,
    CLE.CaseID,
    'Test' AS ContactName,
    EU.UserName As OfficeUser,
    CLE.DateAndTimeOfContact,
    CLC.Category,
    CLE.ContactDetails

FROM
    ContactLogEntries AS CLE
    JOIN
    ContactLogCategories AS CLC
    ON CLE.ContactLogCategoryID = CLC.ContactLogCategoryID
    JOIN
    Control.dbo.EndUsers AS EU
    ON CLE.UserID = EU.EnduserID

Contact Log数据库中有两个实体(ContactLogEntries和ContactLogCategories),另一个数据库中有数据库第一个实体Control.dbo.EndUsers.联系日志可能包含大量记录.我希望能够只显示特定案例的记录.

我的问题分为两部分:

>我可以直接使用 SQL 视图在网页上显示摘要(可能通过将其读入类)

>我可以创建一个与SQL视图等效的代码第一个对象.

您可以使用TableAttribute(数据通知)或Fluent Mappings中的ToTable将实体直接映射到视图…

例如,使用数据注释:

using System;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

public namespace whatever.mynamespace

    [Table("dbo.ContactLogSummaries")] //<-- this is your view
    public class ContactLogSummary
    {
        ...
    }
}

翻译自:https://stackoverflow.com/questions/18595589/using-an-sql-view-from-an-entity-framework-code-first-version-5


以上所述就是小编给大家介绍的《c# – 使用实体框架代码中的SQL视图第一版本5》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Master Switch

The Master Switch

Tim Wu / Knopf / 2010-11-2 / USD 27.95

In this age of an open Internet, it is easy to forget that every American information industry, beginning with the telephone, has eventually been taken captive by some ruthless monopoly or cartel. Wit......一起来看看 《The Master Switch》 这本书的介绍吧!

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具