C# 使用 Entity Framework Code First 版本 5 中的 SQL 视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18595589/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 12:40:55  来源:igfitidea点击:

Using an SQL View from an Entity Framework Code First version 5

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

提问by Peter Smith

I am developing a contact log in a website using VS 2010, MVC3 and EF 5 - the entities are created using code first. The data is stored in an SQL Server 2008 R2 set of databases. I want to display a summary of the contact log and have created a view.

我正在使用 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

There are two entities in the Contact Log database (ContactLogEntriesand ContactLogCategories) and a database first entity Control.dbo.EndUsersin another database. The contact log could contain a large number of records. I want to be able to display just the records for a specific case.

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

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)
  2. Can I create a code first object equivalent to the SQL view.
  1. 是否可以直接使用SQL视图在网页上显示摘要(可能通过读入类)
  2. 我可以创建一个等效于 SQL 视图的代码优先对象吗?

采纳答案by Peter Smith

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 的答案很感兴趣,但我有一个可行的解决方案。

回答by Brandon

You can just map the Entity directly to the view using TableAttribute (data annoations), or ToTable in your Fluent Mappings...

您可以使用 TableAttribute(数据注释)或 Fluent Mappings 中的 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
    {
        ...
    }
}