【问题标题】:Not able to solve this data update issue in mvc5无法在 mvc5 中解决此数据更新问题
【发布时间】: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


【解决方案1】:

你需要更新,在你的情况下,你只追加,这是错误的,因为有相同的对象 ID,所以你可以在之前

db.Savechanges();

添加 db.Entry(YouResultClass).State=System.Data.Entity.EntityState.Modified;

【讨论】:

  • 或使用AddOrUpdate() Insted of Add()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
  • 2021-08-02
  • 2017-10-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多