简体   繁体   English

实体框架6将自定义SQL查询映射到实体

[英]Entity Framework 6 mapping a custom SQL query to an Entity

I am very new to ASP.NET MVC and Entity Framework at the moment. 目前,我对ASP.NET MVC和实体框架还很陌生。 I've been working with it for about 3 months at the current moment in time. 目前,我已经使用了大约3个月。

I have an ASP.NET MVC 5 application that's running Entity Framework 6. This is a code first approach from an existing database with auto migrations enabled so all of my Entity classes are auto generated. 我有一个运行Entity Framework 6的ASP.NET MVC 5应用程序。这是启用了自动迁移的现有数据库中的代码优先方法,因此所有Entity类都是自动生成的。 I am trying to add a view to my MVC application that returns a specific result set. 我试图将视图添加到我的MVC应用程序,以返回特定的结果集。 Currently the previous developer has the application set up to only accept an Entity class to display data to a DataTable DataTables.net . 当前,以前的开发人员已将应用程序设置为仅接受Entity类以将数据显示到DataTable DataTables.net To clarify further: 为了进一步说明:

I have two Entity tables in my model that are tables in MySQL. 我的模型中有两个实体表,它们是MySQL中的表。

| Samples           | SampleLocation |
|:------------------|---------------:|
| Id                |     LocationId |        
| DateAssigned      |           Name |      
| CheckedInDate     |           Size |         
| SampleLocationId  |                |        
| ...etc            |         ...etc |          

What I'm trying to accomplish is querying both tables and returning the results to my MVC application in a view. 我要完成的工作是查询两个表并将结果返回到视图中的MVC应用程序。 From there run an Update and update a couple of columns in the Samples table. 从那里运行一个Update,并更新Samples表中的几列。 Below is the roughly the query that returns the results I need. 以下是大致返回我所需结果的查询。

SELECT Samples.Id, samples.CheckedInDate, SampleLocation.Name, SampleLocation.Size, 
SampleLocation.LocationId 
FROM (Samples join SampleLocation 
ON ((Samples.SampleLocationId = SampleLocation.LocationId))) 
WHERE  isnull(samples.CheckedInDate) ORDER BY samples.Id

From the research that I have done there are a few ways to accomplish this. 根据我所做的研究,有几种方法可以实现此目的。 The ways that I've tried that would give me a class I could use are creating a stored procedure and then updating the model - this breaks the model and unmaps every single entity in the model. 我尝试过的可以给我一个类的方法是创建一个存储过程,然后更新模型-这会破坏模型并取消映射模型中的每个实体。 I have tried creating a view with the query to add to the model - but this breaks it as well and unmaps everything. 我尝试用查询创建一个视图以添加到模型中-但这也会破坏它并取消映射所有内容。 I later found out that this is a bug. 后来我发现这是一个错误。

So my question is, how can I map a query to an Entity that return results to a view? 所以我的问题是,如何将查询映射到将结果返回到视图的实体? Is there a better way to go about this? 有没有更好的方法来解决这个问题?

There are several ways to accomplish what you want to do, using either Entity Framework only or extra tools like Dapper 仅使用实体框架或诸如Dapper之类的其他工具,有几种方法可以完成您想要的工作

Entity Framework Only: 仅实体框架:

First you can use Linq to extract the data, I'll look something like this: 首先,您可以使用Linq提取数据,我将看起来像这样:

var list = from s in Samples
           join l in SampleLocations
               on s.Id equals l.LocationId
           where s.CheckedInDate == null
           select new
           {
                s.Id,
                s.CheckedInDate,
                l.Name,
                l.Size, 
                l.LocationId 
           };

Dapper: 小巧玲珑:

The second method is using dapper, the only real difference here is that you would be working with your queries directly, so instead of linq you had something like this: 第二种方法是使用dapper,这里唯一的真正区别是您将直接使用查询,因此,代替linq的是:

Connection.Query(@"SELECT Samples.Id, samples.CheckedInDate,  
                     SampleLocation.Name, SampleLocation.Size, 
                     SampleLocation.LocationId 
                   FROM Samples
                     join SampleLocation 
                       ON Samples.SampleLocationId = SampleLocation.LocationId
                   WHERE isnull(samples.CheckedInDate) ORDER BY samples.Id");

For both: 对彼此而言:

In your update method, you first have to retrieve the entities, for that you can use the DbSet.Find method or another query, after that you call DbConext.SaveChanges . 在更新方法中,首先必须检索实体,为此,您可以使用DbSet.Find方法或其他查询,然后再调用DbConext.SaveChanges

This is the solution to my problem. 这是我的问题的解决方案。 Basically manually creating an Entity within the XML and mapping it to a virtual table. 基本上是在XML中手动创建一个Entity并将其映射到虚拟表。 Entity Framework DefiningQuery 实体框架定义查询

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

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