当我尝试从表中删除记录时遇到了一些麻烦。我收到错误:
传递的主键值的数量必须与实体上定义的主键值的数量匹配。
该表具有复合主键。他们是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 :(得分:0)
也许您没有为实体声明主键。您可以通过使用[Key]
属性标记主键列来执行此操作。
[Key]
public int cabinCrewId { get; set; }
[Key]
public int flightId { get; set; }
答案 1 :(得分:0)
public ActionResult Demodelete(int id)
{
Employee emptbl = new Employee();
emptbl.EmpId = id;
dbc.Entry(emptbl).State = EntityState.Deleted;
dbc.SaveChanges();
return View();
}