2014-09-24 48 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(); 
    }