mvc4 key error已添加具有相同键的项目

时间:2013-11-27 22:41:19

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

已添加具有相同密钥的项目。

当我尝试提交时,我得到的错误,我知道它是因为我在workhoursmodel中有两个同名的行,但我怎么能修复它 - 有没有任何简单的方法来修复或做我必须打破桌面并重新创建它?

ps我对mvc真的很新,所以有点帮助就是很好的ds

我的控制器

   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; }
        }
    }

我的观点

 @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)
        <div class="editor-label">
            @Html.LabelFor(model => model.Users)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.Users)
            @Html.ValidationMessageFor(model => model.Users)
        </div>

     <div class="editor-field">
     @Html.DropDownListFor(model => model.Id, Model.Workstations, string.Empty)
    </div>
    <p>
        <input type="submit"  value="Create" />
    </p>

 }

使用数据库信息更新了问题

CREATE TABLE [dbo].[WorkhoursModels] (
    [Id]           INT            IDENTITY (1, 1) NOT NULL,
    [Start]        NVARCHAR (50) NULL,
    [Stop]         NVARCHAR (50) NULL,
    [command]      NVARCHAR (50) NULL,
    [users]        NVARCHAR (MAX) NULL,
    [WorkStations] NVARCHAR (50) NULL,
    CONSTRAINT [PK_dbo.WorkhoursModels] PRIMARY KEY NONCLUSTERED ([Id] ASC)
);

3 个答案:

答案 0 :(得分:1)

我设法找到了解决方案。

我将此添加到我的workhoursmodel

 public virtual WorkStation WorkStation { get; set; } 

然后我将其添加到我的工作站模型

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

然后我修复了我的选择列表

    ViewBag.WorkStation = new SelectList(db.WorkStation, "Id", "WorkStation");
    return View();

答案 1 :(得分:0)

如果记录存在,则数据库中的密钥看起来正常,否则数据库中已存在密钥,否则添加。见例子

处理添加和更新案例

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();
}

答案 2 :(得分:0)

[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