简体   繁体   English

首先使用C#实体框架数据库-仅使用某些字段

[英]C# Entity Framework Database First - use only some fields

I am creating a site using EF database-first with ASP.NET MVC (I follow all of this tutorial: https://www.asp.net/mvc/overview/getting-started/database-first-development/creating-the-web-application ) and everything is working ok. 我先使用EF数据库和ASP.NET MVC创建一个站点(我遵循了本教程的全部内容: https : //www.asp.net/mvc/overview/getting-started/database-first-development/creating-the -web-application ),一切正常。

The problem is that in the index view (the list of records) I want to remove one of the fields from the the entity. 问题是在索引视图(记录列表)中,我想从实体中删除字段之一。 I don't just want it to be hidden in the views, but all together removed (the issue is that it holds a very big chunk of data, which make the loading of the list very slow). 我不仅希望将其隐藏在视图中,还希望将其全部删除(问题是它包含很大的数据块,这使得列表的加载非常缓慢)。

On the "edit" view I do want to show and use all fields. 我确实想在“编辑”视图上显示和使用所有字段。

If I remove one field from the the entity, I get this error: 如果我从实体中删除一个字段,则会出现此错误:

The entity type XXX is not part of the model for the current context. 实体类型XXX不是当前上下文模型的一部分。

on

 return View(db.XXX.ToList());

What can I do? 我能做什么?

There are a couple of ways you can go about this. 有两种方法可以解决此问题。 The issue, at hand, is the .ToList(). 当前的问题是.ToList()。 If you just do db.XXX, then you will obtain an Enumerable, which, until it is enumerated, is just a set of instructions to grab data from the DB. 如果您只是执行db.XXX,那么您将获得一个Enumerable,在枚举之前,Enumerable只是一组从DB抓取数据的指令。 Once you call .ToList() it will actually go and grab the data from the DB, which is the step that is taking so long. 一旦调用.ToList(),它将实际上从数据库中获取数据,这是花费了很长时间的步骤。

The best thing to do, in my opinion is define a ViewModel that contains all of the fields except the one with the large amount of data. 我认为最好的做法是定义一个ViewModel,其中包含除具有大量数据的字段以外的所有字段。

public class ViewModel
{ 
     public ViewModel(){}
     public int Id {get;set;}
     public string OtherData {get;set;}
}

Move the db.XXX outside of the View function as so: 将db.XXX移到View函数之外,如下所示:

var initialDBObject = db.XXX;

From there you can take your Enumerable (which is still just a set of instructions to access data from your DB) and Select it into your ViewModel object like so: 从那里可以使用Enumerable(它仍然只是从数据库访问数据的一组指令),然后将其选择到ViewModel对象中,如下所示:

var viewModelObject = initialDBObject.Select(x=> new ViewModel
    {
        Id = x.Id;
        OtherData = x.OtherData;
        //do not add the large column of data to the ViewModel
    });

What is happening here (before calling ToList()) is you are modifying the query that linq generates behind the scenes to grab the data from table XXX (if you put a break-point on this line and hover over initialDBObject you will see the SQL that gets generated). 这里发生的事情(在调用ToList()之前)是您正在修改linq在幕后生成的查询以从表XXX中获取数据(如果在此行上放置断点并将鼠标悬停在initialDBObject上,您将看到SQL生成)。 Instead of just grabbing the data from table XXX, the query will grab the data and insert it into a ViewModel object (instead of the XXX object, as defined in your .edmx file) once you call ToList(). 一旦调用ToList(),查询将获取数据并将其插入到ViewModel对象(而不是XXX对象,由您的.edmx文件定义)中,而不仅仅是从表XXX中获取数据。

You can also 你也可以

.Select(x=> new 
{
    Id, 
    OtherData
});

and create an anonymous object, but getting an anonymous object to work in a View is a little complicated. 并创建一个匿名对象,但是要使一个匿名对象在View中工作有点复杂。

Then you need to update the Index View page to use the ViewModel instead of the original DB object and you can pass it like: 然后,您需要更新“索引视图”页面以使用ViewModel而不是原始数据库对象,并且可以像这样传递它:

View(viewModelObject.ToList()); 

Name the ViewModel something besides ViewModel, though. 但是,将ViewModel命名为ViewModel之外的其他名称。 Like [DB Table Name]ViewModel or something similar. 类似于[数据库表名称] ViewModel或类似名称。

If you have lots of rows in the DB it will still take a long time to load all of the data, in which case you need to look into Paging. 如果数据库中有很多行,则加载所有数据仍将花费很长时间,在这种情况下,您需要研究分页。

There is no problem with ToList(), on its own, the original issue was caused by the call to ToList() because it is at that point in the code that the program goes to the database and uses the linq-generated query to grab the data. ToList()本身没有问题,最初的问题是由对ToList()的调用引起的,因为正是在该点上,程序才进入数据库并使用linq生成的查询来捕获数据。 If you are trying to ToList() an entire table's worth of data, or as in your case, have a column with a huge chunk of data, it may take some time or you may run out of memory. 如果您尝试ToList()整个表的数据价值,或者像您的情况一样,创建一列包含大量数据的列,则可能要花一些时间,或者可能会用完内存。

Regarding the 15 columns that you have to include in the .Select(), yes, that is annoying. 关于您必须在.Select()中包含的15列,是的,这很烦人。 Unfortunately, you cannot use a constructor in a linq statement, so are forced to populate each column. 不幸的是,您不能在linq语句中使用构造函数,因此被迫填充每一列。

Another alternative to defining a ViewModel, which may be a little bit easier is to open up the .edmx design surface, right-click the background and Add New->Entity. 定义ViewModel的另一种方法(可能会更简单一些)是打开.edmx设计图面,右键单击背景并添加New-> Entity。 You can use table XXX as a base, give it a different name, like XXXViewModel or whatever and then delete the column containing the large amount of data. 您可以使用表XXX作为基础,给它一个不同的名称,例如XXXViewModel或其他名称,然后删除包含大量数据的列。 Then you just need to do db.XXXViewModel.ToList(). 然后,您只需要执行db.XXXViewModel.ToList()。

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

相关问题 是否可以在不使用Entity Framework中以代码优先方式使用C#属性的情况下生成数据库字段? - Is it possible to generate database fields without using C# properties in Entity Framework, code-first approach? 具有类/数据库的循环引用(C#,首先是实体框架代码) - Circular reference with Classes/Database (C#, Entity Framework Code First) C#Entity Framework数据库列名称中的第一个符号 - C# Entity Framework database first symbol in column name C#实体框架6上下文对象数据库首先具有现有DbConnection - C# Entity Framework 6 Context Object Database First with existing DbConnection C# 实体框架:仅更新第一批记录并停止 - C# Entity Framework: Update Only First Batch Records and Stop 实体框架仅保存一些字段 - Entity Framework only saves some fields 实体框架只保存我的一些字段 - Entity Framework only saving some of my fields 如何在不将第一个实体插入Entity Framework C#中的数据库的情况下将实体ID分配给另一个实体? - How to assign entity Id to another entity without inserting first entity into database in Entity Framework, C#? 在C#中的实体框架中选择“多字段” - Select Multi fields in Entity framework in c# 首先使用实体​​框架代码与nosql数据库 - Use Entity framework code first with nosql database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM