2014-09-24 82 views
0

当我尝试从表中删除记录时遇到了一些麻烦。我收到错误:删除表中的记录MVC 4

传递的主键值数量必须与实体上定义的主键值数量匹配。

表中有一个复合主键。他们是cabinCrewId和flightId。

控制器:

public ActionResult Delete(string name) 
    { 
     using (A1Context db = new A1Context()) 
     { 
      var id = from person in db.person 
         from flightcabincrew in db.flightcabincrew 
         from cabincrew in db.cabincrew 
         where person.name == name 
         where person.id == cabincrew.person 
         where cabincrew.person == flightcabincrew.cabinCrewId 
         select new { flightcabincrew.cabinCrewId, flightcabincrew.flightId }; 

      FlightCabinCrew fcc = db.flightcabincrew.Find(id); 
      if (fcc == null) 
      { 
       return HttpNotFound(); 
      } 
      else 
      { 
       return View(fcc); 
      } 
     } 
    } 

帖子:视图的

[HttpPost] 
    public ActionResult Delete(FlightCabinCrew fcc) 
    { 
     using (A1Context db = new A1Context()) 
     { 
      db.Entry(fcc).State = System.Data.EntityState.Deleted; 
      db.SaveChanges(); 
     } 
     return View(); 
    } 

和部分有关:

@model IEnumerable<Assignment2.Models.FlightCrewGrid> 
     @{ 
      WebGrid grid = new WebGrid(Model); 
     } 
     <h2>@ViewBag.Title</h2> 
     @grid.GetHtml(columns: grid.Columns(
      grid.Column("PersonName", "Crew Member"), 
      grid.Column("FlightDay", "Flight Day"), 
      grid.Column("FromAirport", "From"), 
      grid.Column("ToAirport", "To"), 
      grid.Column("Model", "Model"), 
      grid.Column("startDate", "Start Date"), 
      grid.Column(header: "Delete", format: (item) => Html.ActionLink("Delete", "Delete", new { id=item.PersonName}))) 

先感谢您的任何指导。

回答

0

也许你没有声明实体的主键。通过使用[Key]属性标记主键列来完成此操作。

[Key] 
public int cabinCrewId { get; set; } 

[Key] 
public int flightId { get; set; } 
+0

感谢您的答复,但我有我的模式,将[按键] [列(订单= 1)]的第一和Oreder = 2秒。 – user3539718 2014-09-24 23:01:38

0

公众的ActionResult Demodelete(INT ID)

{ 
     Employee emptbl = new Employee(); 
     emptbl.EmpId = id; 
     dbc.Entry(emptbl).State = EntityState.Deleted; 
     dbc.SaveChanges(); 

     return View(); 
    }