Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# mvc4密钥错误已添加具有相同密钥的项 已添加具有相同密钥的项。_C#_Asp.net_Asp.net Mvc 4_Keyerror - Fatal编程技术网

C# mvc4密钥错误已添加具有相同密钥的项 已添加具有相同密钥的项。

C# mvc4密钥错误已添加具有相同密钥的项 已添加具有相同密钥的项。,c#,asp.net,asp.net-mvc-4,keyerror,C#,Asp.net,Asp.net Mvc 4,Keyerror,这就是我在尝试提交时遇到的错误,我知道这是因为我在workhoursmodel中有两行名称相同,但是我如何修复它-有没有简单的方法修复它,或者我必须分解表并重新创建它 ps我对mvc真的很陌生,所以一点帮助会很好 我的控制器 public ActionResult Create() { WorkhoursModels model = new WorkhoursModels(); model.Workstations = db.WorkStation.

这就是我在尝试提交时遇到的错误,我知道这是因为我在workhoursmodel中有两行名称相同,但是我如何修复它-有没有简单的方法修复它,或者我必须分解表并重新创建它

ps我对mvc真的很陌生,所以一点帮助会很好

我的控制器

   public ActionResult Create()
    {
        WorkhoursModels model = new WorkhoursModels();
        model.Workstations = db.WorkStation.AsEnumerable().Select(x => new SelectListItem() { Text = x.WorkStations, Value = x.Id.ToString() });
        return View(model);
    }


    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(WorkhoursModels workhoursmodels)
    {
        db.WorkHours.Add(workhoursmodels);
        db.SaveChanges();
        return View();
    }

{
我的模型

{
    [Table("WorkHoursModels")]
    public class WorkhoursModels
    {
        public int Id { get; set; }
        public string Start { get; set; }
        public string Stop { get; set; }
        public string command { get; set; }
        public string Users { get; set; }
        public IEnumerable<System.Web.Mvc.SelectListItem> Workstations { get; set; }
        public string WorkStations { set; get; }
    }
        [Table("WorkStations")]
        public class WorkHoursStation
        {
            public int Id { get; set; }
            public string WorkStations { get; set; }
            public IEnumerable<SelectListItem> Workstation { get; set; }
        }



        public class WorkhourDB : DbContext 
        {
            public DbSet<WorkhoursModels> WorkHours { get; set; }
            public DbSet<WorkHoursStation> WorkStation { get; set; }
        }
    }

数据库中已有一个键,如果该键在数据库中看起来正常,则更新该记录,否则添加。参见示例

处理添加和更新案例

Handle both the add and update cases

public void Persist(Company company)
{
  var companyInDb = _dbSet.SingleOrDefault(c => c.Id == company.Id);
  if (companyInDb != null)
  {
     _context.Entry(companyInDb).CurrentValues.SetValues(company);
  }
  else
  {
    _dbSet.Add(company);
  }
    _context.SaveChanges();
}

我设法找到了一个解决办法

我将此添加到我的工时模型中

 public virtual WorkStation WorkStation { get; set; } 
然后我把它添加到我的工作站模型中

public virtual ICollection<Workhourmodels> Workhourmodels{ get; set; }

是否在要添加到的表上设置了自动增量键?否-仅通过将自动增量添加到工作站?IEnumerable无法写入数据库。它必须是某个类名。如果您的WorkHours表在主键列上的Identity specification属性设置为Yes,请在数据库中进行检查。Alexey Aza我提供了数据库信息,当您查找现有键并保存更改时会发生什么情况,会发生什么错误?
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(WorkhoursModels workhoursmodels)//object workhoursmodels object has some connection with 
{
    db.WorkHours.Add(workhoursmodels);
    db.SaveChanges();
    return View();
}
//try to create a new object workhoursmodel and get all information you need from the object post from view
//eg:
WorkHours workHours = new WorkHours();
workHours.Users = workhoursmodels.Users
 public virtual WorkStation WorkStation { get; set; } 
public virtual ICollection<Workhourmodels> Workhourmodels{ get; set; }
    ViewBag.WorkStation = new SelectList(db.WorkStation, "Id", "WorkStation");
    return View();