简体   繁体   English

从Entity Framework Code First版本5使用SQL视图

[英]Using an SQL View from an Entity Framework Code First version 5

I am developing a contact log in a website using VS 2010, MVC3 and EF 5 - the entities are created using code first. 我正在使用VS 2010,MVC3和EF 5在网站中开发联系人日志-首先使用代码创建实体。 The data is stored in an SQL Server 2008 R2 set of databases. 数据存储在SQL Server 2008 R2数据库集中。 I want to display a summary of the contact log and have created a 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

There are two entities in the Contact Log database ( ContactLogEntries and ContactLogCategories ) and a database first entity Control.dbo.EndUsers in another database. 联系人日志数据库中有两个实体( ContactLogEntriesContactLogCategories ),另一个数据库中的数据库第一个实体Control.dbo.EndUsers The contact log could contain a large number of records. 联系人日志可能包含大量记录。 I want to be able to display just the records for a specific case. 我希望能够仅显示特定案例的记录。

My question is in two parts: 我的问题分为两个部分:

  1. Can I use the SQL view directly to display a summary on a web page (perhaps by reading it into a class) 我可以直接使用SQL视图在网页上显示摘要(也许通过将其读入类中)
  2. Can I create a code first object equivalent to the SQL view. 我可以创建一个等效于SQL视图的代码第一个对象。

You can just map the Entity directly to the view using TableAttribute (data annoations), or ToTable in your Fluent Mappings... 您可以使用Fluent Mappings中的TableAttribute(数据注释)或ToTable将实体直接映射到视图。

For example using data annotions: 例如,使用数据注释:

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

Found a simple solution to question 1: 找到问题1的简单解决方案:

public class ContactLogSummary
{
    public int ContactLogEntryID { get; set; }
    public int MaternalCaseID { get; set; }
    public String ContactName { get; set; }
    public String OfficeUser { get; set; }
    public DateTime DateAndTimeOfContact { get; set; }
    public String Category { get; set; }
    public String ContactDetails { get; set; }

    public static List<ContactLogSummary> LoadContactListSummary
                                             (int caseID, String connectionString);
    {
        MyDataContext dbContext = new MyDataContext(connectionString);
        return dbContext.Database.SqlQuery<ContactLogSummary>
               ("SELECT * FROM dbo.ContactLogSummaries WHERE MaternalCaseID = @CaseID ORDER BY ContactLogEntryID DESC",
                                     new SqlParameter("CaseID", caseID)).ToList();
    }

It does all that's required so, although I'm interest in an answer to question 2 I have a working solution. 它可以满足所有要求,因此尽管我对问题2的答案很感兴趣,但我有一个可行的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM