简体   繁体   English

数据库查询仅返回数据库的第一个元素作为所有相同的实例,在 ASP.NET MVC Core 6

[英]Database query returning only the first element of the data base as the same instance for all, in ASP.NET MVC Core 6

I am having some trouble and cannot figure out why my sql database is returning the first data entry as all the other entries.This is my method to get all the database table entries from the table called HealthDataFuture我遇到了一些麻烦,无法弄清楚为什么我的 sql 数据库返回第一个数据条目作为所有其他条目。这是我从名为 HealthDataFuture 的表中获取所有数据库表条目的方法

[HttpPost]
        public IActionResult AjaxMethod(string id)
        {
            List<object> chartData = new List<object>();
            chartData.Add(new object[]
                            {
                            "Months", "SOFAS"
                            });
            //FOR SOME REASON IT KEEPS READING THE FIRST DATABASE ENTRY ONLY SO THERE ALL THE ENTRIES ARE THE SAME VALUE
            
            foreach (HealthDataFuture data in _db.HealthDataFuture)
            {
                if (data.id == id)
                {
                    chartData.Add(new object[]
                        {
                            data.Month, data.Sofas
                        });
                }
            }

            return Json(chartData);
        }

这是我的模型

This is my database table entries, it's returning the first entry as all the other ones when I query all of it, they have the same id because i want to return all of them and then graph这是我的数据库表条目,当我查询所有条目时,它会像所有其他条目一样返回第一个条目,它们具有相同的 ID,因为我想返回所有条目,然后返回图表

这是我的数据库表条目,它也像所有其他条目一样返回第一个条目

This is the results i keep getting back这是我不断回来的结果

这是我不断回来的结果

i have also tried this way of getting data however it is the same problem我也尝试过这种获取数据的方式,但这是同样的问题

-------EDIT ----- PROBLEM RESOLVED It turns out my MYSQL table model was not created properly as there was no primary key nor foreign key ------编辑 ----- 问题已解决原来我的 MYSQL 表 model 没有正确创建,因为没有主键也没有外键

It's better not to share image, but code using correct tag.最好不要共享图像,而是使用正确的标签进行编码。 To use code helps other users and all the cummunity, for example it makes your question searchable.使用代码可以帮助其他用户和所有社区,例如,它可以使您的问题可搜索。

Anyway you can try to do in this way:无论如何你可以尝试这样做:

if (!string.IsNullOrEmpty(id)){
    List<HealthDataFuture> DataList = _db.HealthDataFuture.Where(x => h.Id == id).ToList();
    return View(id);
}
return RedirectToAction("Index");

I've also some question about your code:我对您的代码也有一些疑问:

  1. What should it do?它应该做什么?
  2. DataList is like a select query from db, but nobody is using DataList. DataList 就像来自 db 的 select 查询,但没有人使用 DataList。 Why?为什么?

The more details you provide, the more information we have to help you.您提供的详细信息越多,我们为您提供的帮助信息就越多。

Editing after comments:评论后编辑:

If you want to remove data from db you should use saveChanges.如果你想从数据库中删除数据,你应该使用 saveChanges。 For example, if you want to remove all lines with id other than "1", you can try:例如,如果你想删除所有 id 不是“1”的行,你可以尝试:

if (!string.IsNullOrEmpty(id)){
    _db.HealthDataFuture.RemoveRange(_db.HealthDataFuture.Where(x => h.Id != id));
    _db.SaveChanges();
    return View(id);
}
return RedirectToAction("Index");

You can also read something about access to DB in async way, it's recommended and it perfoms better: https://learn.microsoft.com/it-it/ef/core/miscellaneous/async您还可以阅读有关以异步方式访问数据库的内容,推荐它并且性能更好: https://learn.microsoft.com/it-it/ef/core/miscellaneous/async

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

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