首页 > 解决方案 > 实体框架核心(使用 mysql)模型首先 apporach 如何运行自定义查询以获得一些计数

问题描述

我在我的存储库中使用实体框架核心获取不同的模型 get/add/update/delete 和 getall 现在对于一种情况,我需要纠正自定义查询以获取计数。我找不到任何可以运行自定义查询的方法。有没有人用过。

标签: c#mysql.net-coreentity-framework-core

解决方案


创建数据传输对象 (DTO),例如。

 public class yourclassname
    {
        public string yourfiledname { get; set; }
        public int count { get; set; }
    }

然后你

public IActionResult gettop10staffcount()
    {
        var query = "your query";
        var result = Context.yourclassname.FromSql(query).ToList();
        return new ObjectResult(result);
    }
       

https://docs.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5


推荐阅读