c# - 使用 Entity Framework Code First 版本 5 中的 SQL View

标签 c# asp.net-mvc-3 sql-server-2008 entity-framework-5

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

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

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

我的问题分为两部分:

  1. 我可以直接使用 SQL View 在网页上显示摘要吗(也许通过将其读入类)
  2. 我可以创建与 SQL View 等效的代码优先对象吗?

最佳答案

您可以使用 TableAttribute(数据注释)或 Fluent 映射中的 ToTable 将实体直接映射到 View ...

例如使用数据注释:

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
    {
        ...
    }
}

关于c# - 使用 Entity Framework Code First 版本 5 中的 SQL View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18595589/

相关文章:

c# - 在 asp.net c# 中隐藏和取消隐藏标签

c# - 如何处理大型 WCF 响应?

mysql - EF4.1/MVC3 数据库第一个 : How to remove password from connection string

asp.net-mvc - 我有理由使用 Knockout MVC 而不是 Knockout JS 吗?

sql - sql server中的3d空间对象

c# - 如何在我的 asp.net 项目中使用动态 LINQ?

c# - 从 MediaElement 控件读取自定义编码的网络流

javascript - 使用jquery禁用按钮停止向chrome中的服务器发送输入--MVC

sql-server - 如何在 SQL 2008 中更改列而不删除表

java - 我需要为 Java 代码创建一个独立的数据库(最好是 SQL Server)