【发布时间】:2021-07-10 14:33:14
【问题描述】:
我无法解决这个错误
似乎是数据更新问题。如何解决?
结果控制器:
> using E_voting.Models.DataContext; using System; using
> System.Collections.Generic; using System.Linq; using System.Web; using
> System.Web.Mvc;
>
> namespace E_voting.Controllers {
> public class ResultController : Controller
> {
> private EvotingDBContext db = new EvotingDBContext();
> // GET: Result
> public ActionResult Index() {
> db.Configuration.LazyLoadingEnabled = false;
>
> Dictionary<string, int> counts = new Dictionary<string, int>();
> foreach (var item in db.Result.ToList())
> {
> string id = item.CandidateId.ToString();
> KeyValuePair<string, int> temp = new KeyValuePair<string, int>(id.ToString(), 1);
> if (counts.ContainsKey(id.ToString()))
> {
> int index = Array.IndexOf(counts.Keys.ToArray(), id.ToString());
>
>
>
> counts[id.ToString()] = counts[id.ToString()] + 1;
> }
> else
> counts.Add(id.ToString(), 1);
> }
>
> int v = counts.Values.Max();
> int tempmax = v;
> string tempkey = "";
> foreach (var item in counts)
> {
> if (item.Value == tempmax)
> {
> tempkey = item.Key;
> break;
> }
> }
> string tempname = "a";
> foreach (var item in db.Candidate)
> {
> if (item.CandidateId.ToString() == tempkey)
> {
> tempname = item.Name;
> break;
> }
> }
> ViewBag.winnerid = tempkey;
> ViewBag.number = tempname;
> ViewBag.winnercount = tempmax.ToString();
> return View(db.Result.ToList());
> }
>
> /*Count*/ // Dictionary<string, int> counts = new Dictionary<string, int>(); // foreach (var item in
> db.Result.ToList()) // { // int id =
> item.CandidateId; // KeyValuePair<string, int> temp = new
> KeyValuePair<string, int>(id.ToString(), 1); // if
> (counts.ContainsKey(id.ToString())) // { //
> int index = Array.IndexOf(counts.Keys.ToArray(), id.ToString());
>
> // counts[id.ToString()] = counts[id.ToString()] + 1; //
> } // else //
> counts.Add(id.ToString(), 1); // } //
> int tempmax = counts.Values.Max();
>
> } }
>
>
>
>
> >
> > the above is the controller class and the error is in image . this is
> > result model class :
> >
> > using System; using System.Collections.Generic; using
> > System.ComponentModel.DataAnnotations; using
> > System.ComponentModel.DataAnnotations.Schema; using System.Linq; using
> > System.Web;
> >
> > namespace E_voting.Models.Model {
> >
> >
> > [Table("Result")]
> >
> >
> > > public class Result
> > > {
> > > [Key]
> > > public int VoteCastingId { get; set; }
> >
>
> public int CandidateId { get; set; } > > public int VoterId { get; set; } > } }
如果有人指导我如何解决这个问题,也会给出错误图像 它显示,结果控制器类中的系统数据实体基础结构数据库更新异常。如果有人指导我如何解决这个问题,也会给出错误图像 它显示,结果控制器类中的系统数据实体基础设施数据库更新异常。
【问题讨论】:
-
这个错误告诉我们更新影响了 0 行。这应该是对问题的非常清楚的描述。既然你没有提供任何细节......其他人怎么能猜到是什么原因造成的?
-
如果您需要帮助,请发布您的 Result 课程。
-
这是结果类:
-
我在 ToVote 方法中没有看到
db.Result.Add
标签: asp.net-mvc model-view-controller