【问题标题】:ASP.NET Core web application - display database data C#ASP.NET Core Web 应用程序 - 显示数据库数据 C#
【发布时间】:2020-04-15 19:26:20
【问题描述】:

我正在尝试显示来自 SQL Server 数据库的数据。我已经为此苦苦挣扎了一整天,仍然找不到任何可行的解决方案或教程。

我想做的——做一个简单的“数据库浏览器”。到目前为止,最好的工作是本教程https://www.c-sharpcorner.com/article/entity-framework-database-first-in-asp-net-core2/

但是我只有一张表要显示,不知道这部分代码怎么写:

 public IActionResult Index()  
 {  
     var _emplst = _dbContext.tblEmployees
                             .Join(_dbContext.tblSkills, e => e.SkillID, s => s.SkillID,  
                                   (e, s) => new EmployeeViewModel  
                                                 { EmployeeID = e.EmployeeID, EmployeeName = e.EmployeeName,  
                                                   PhoneNumber = e.PhoneNumber, Skill = s.Title,  
                                                   YearsExperience = e.YearsExperience }).ToList();  

     IList<EmployeeViewModel> emplst  = _emplst;  
     return View(emplst);  
}  

只有一个表(没有任何连接)。我尝试的所有操作都以无法将tblEmployees 转换为EmployeeViewModel 的错误告终。

有人可以帮助我吗?或者建议任何其他可能有效的解决方案?我真的只是想从表格中拖动数据并将其显示在网页上。

编辑: ComponentContext.cs:

public class ComponentsContext:DbContext
    {
        public ComponentsContext(DbContextOptions<ComponentsContext> options) : base(options)
        {

        }

        public DbSet<tblComponents> tblComponent { get; set; }

    }
}

【问题讨论】:

  • 也许对于这样一个“通用”的数据库浏览器,Entity Framework 可能不是最佳选择。也许你需要回到“老派”的低级 ADO.NET 来处理这样的场景......

标签: c# asp.net asp.net-mvc


【解决方案1】:

您的_emplst 列表的类型(类)与EmployeeViewModel 的类型(类)不同。

所以你需要遍历你的列表_emplst 并转移EmployeeViewModel 中需要的值。

这可能是这样的:

     public IActionResult Index()  
 {  
            var _emplst = _dbContext.tblEmployees.  
                            Join(_dbContext.tblSkills, e => e.SkillID, s => s.SkillID,  
                            (e, s) => new EmployeeViewModel  
                            { EmployeeID = e.EmployeeID, EmployeeName = e.EmployeeName,  
                                PhoneNumber = e.PhoneNumber, Skill = s.Title,  
                                YearsExperience = e.YearsExperience }).ToList();  
            var emplst  = _emplst.Select( e=> new EmployeeViewModel {
               .. i dont known the properties ..
               A = e.A,
               B = e.B,
               ... .. 
            }).ToList();  
            return View(emplst);  
  }

作为对tlbComponent 下方评论的回答,试试这个:

var _cmplist = _dbContext.tblComponent.ToList().Select(e => new ComponentsViewModel { ID = e.ID, Name = e.Name, } ).ToList();
IList<ComponentsViewModel> cmplist = _cmplist;
return View(cmplist);

我已将_dbContext.tblComponent.Select(... 更改为_dbContext.tblComponent.ToList().Select(...

【讨论】:

  • 我尝试了以下操作: var _cmplist = _dbContext.tblComponent.Select(e => new ComponentsViewModel { ID = e.ID, Name = e.Name, } ) .ToList(); IList cmplist = _cmplist;返回视图(cmplist);但它以“无效的对象名称(tblComponent)”结束
  • 它表示您在_dbContext 的类型上没有DbSet&lt;TblComponent&gt; tblComponent {get;set;}。在您的问题中显示_dbContext 的类型,我们可以为您提供帮助。
  • 公共类 ComponentsContext:DbContext { public ComponentsContext(DbContextOptions options) : base(options) { } public DbSet tblComponent { get;放; } } } 谢谢你的回复:)
  • ComponentsContext 类更新你的问题,它更具可读性;)
猜你喜欢
  • 2013-07-23
  • 1970-01-01
  • 1970-01-01
  • 2016-02-12
  • 2020-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-03
相关资源
最近更新 更多