简体   繁体   English

使用 EF 6 访问数据库的方法?

[英]approach for DB access with EF 6?

Looking for approach for DB access with EF 6. i am using .net MVC 5 and i have this code:寻找使用 EF 6 进行数据库访问的方法。我正在使用 .net MVC 5,我有这个代码:

    public IEnumerable<string> GetSPSites()
    {
        List<string> spSites = new List<string>();

        using (var context = new DevPortalDBContext())
        {
            var sites = context.Spsites;
            foreach(var item in sites)
            {
                spSites.Add(item.Url);
            }
        }

        return spSites;

    }

But i guess this approach for accessing the db every time is not so good.但我想这种每次访问数据库的方法都不是很好。 i would like to know if there is a better way or something like dependency injection that i can do in the constructor.我想知道是否有更好的方法或类似依赖注入的方法可以在构造函数中执行。 please let me know, Thank you.请告诉我,谢谢。 (* new to .Net *) (* .Net 的新功能 *)

my assumption is that 100 users will use this code at parallels.我的假设是 100 个用户将同时使用此代码。

If you are for performance, your code isn't exactly that.如果您是为了性能,那么您的代码并非完全如此。

But why is it?但这是为什么呢?
Well, good question.嗯,好问题。
First of all, EF is loading all items from the whole table and we are really only using Spsites.Url.首先,EF 正在加载整个表中的所有项目,而我们实际上只使用 Spsites.Url。 So what could we do to improve this behavior?那么我们可以做些什么来改善这种行为呢? We could use Linq (But you should be sure that you are using IQueryable instead of IEnumerable which could come at a cost)我们可以使用 Linq (但您应该确定您使用的是 IQueryable 而不是 IEnumerable,这可能需要付出代价)

public IEnumerable<string> GetSPSites()
{
    return context.Spsites.Select(x => x.Url).ToList();
}

Ok, now that we are just loading the data that is required, could we do something else to improve throughput?好的,现在我们只是加载了所需的数据,我们可以做些其他的事情来提高吞吐量吗? Yes.是的。 We could use async code using async/await.我们可以使用 async/await 来使用异步代码。

public async Task<IEnumerable<string>> GetSPSites()
{
    return await context.Spsites.Select(x => x.Url).ToListAsync();
}

But what does this magical await do?但是这个神奇的等待有什么作用呢?
Well, on a low level, you are talking to a server over the wires.好吧,在低层次上,您正在通过电线与服务器交谈。 What synchronous code does, is waiting for the response.同步代码的作用是等待响应。 This means, that the current thread sleeps until the response from the sql server comes in, and we dont want to do this, because threads are really expensive in terms of performance and throughput.这意味着,当前线程休眠直到来自 sql 服务器的响应进来,我们不想这样做,因为线程在性能和吞吐量方面确实很昂贵。 await changes your code behind the scenes in that way, that your current thread can do other workloads (like serving other requests) while waiting for a response from the sql server. await 以这种方式在后台更改您的代码,您当前的线程可以在等待来自 sql 服务器的响应时执行其他工作负载(例如服务其他请求)。

But be careful: There are things you should be aware of.但要小心:有些事情你应该注意。 One of those is, that you have to switch to async programming all the way to the "root" of your program to improve the performance of your program.其中之一是,您必须一直切换到异步编程到程序的“根”,以提高程序的性能。

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

相关问题 EF Db First方法-处理不带PK的表 - EF Db First approach - dealing with tables without PK's 数据库更改使用EF 6以Mvc 5代码优先的方式更改代码 - change in DB changes code in Mvc 5 code first approach using EF 6 在Web Api,服务层和数据库中检查用户访问权限的更好方法 - Better approach to check users access in Web Api, Service layer, DB 如何在DB First方法中不指定EF Core Fluent API的HasComputedColumnSql中指定SQL表达式? - How to not specify SQL expression in HasComputedColumnSql of EF Core Fluent API in DB first approach? 如何使用 EF Core DB first 方法在 .NET 5 Web API 中创建模型 - How to create models in .NET 5 Web API using EF Core DB first approach 如何在 c# ef5 db first 方法中使用 IntersectionTable 更新实体? - How to update entity with IntersectionTable in c# ef5 db first approach? 在多个不同的带有EF内核的DB环境中,需要访问Connection state - In multiple different DB enviroment with EF core, need to access Connection state EF中一对多的最佳方法 - Best approach for one to many in EF EF和MVC-携手合作的方法 - EF and MVC - approach to work together EF Code First关系法 - EF Code First approach on relationships
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM