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

这一生的挚爱 提交于 2019-12-03 01:29:57

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

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:

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!