此代碼已爲我工作過,但我不知道什麼一直導致此錯誤。我唯一的猜測是,當我嘗試創建一個播放器時,團隊數據被髮送回Team Table並嘗試複製,但是由於TeamId是唯一的,所以這個錯誤。什麼原因INSERT語句與FOREIGN KEY約束衝突?
錯誤
INSERT語句衝突與外鍵約束 「FK_dbo.Players_dbo.Teams_TeamId」。衝突發生在數據庫「Web」,表「dbo.Teams」,列「TeamId」中。該語句已終止。
球員
public class Player
{
...
...
[HiddenInput(DisplayValue = false)]
[ForeignKey("Team")]
public int TeamId { get; set; }
public virtual Team Team { get; set; }
...
}
隊
public class Team
{
[Key]
[HiddenInput(DisplayValue = false)]
public int TeamId { get; set; }
....
public virtual ICollection<Player> Players { get; set; }
}
控制器
//
// GET: /Player/
[HttpGet]
public ActionResult Create()
{
PopulateTeamsDropDownList();
return View();
}
//
// POST: /Player/
[HttpPost]
public ActionResult Create(Player model)
{
if (ModelState.IsValid)
{
using (var db = new EfDb())
{
var userProfile = db.UserProfiles.Single(u => u.UserName == User.Identity.Name);
if (userProfile != null)
{
var player = new Player
{
UserProfile = userProfile,
....
....
};
db.Players.Add(player);
db.SaveChanges();
}
}
}
PopulateTeamsDropDownList(model.TeamId);
return View(model);
}
private void PopulateTeamsDropDownList(object selectedTeams = null)
{
var teamsQuery = from d in _iService.Teams
orderby d.Name
select d;
ViewBag.TeamID = new SelectList(teamsQuery, "TeamId", "Name", selectedTeams);
}
查看
<div class="editor-label">
@Html.LabelFor(model => model.TeamId, "Pick Your Team")
</div>
<div class="editor-field">
@Html.DropDownList("TeamId", String.Empty)
@Html.ValidationMessageFor(model => model.TeamId)
</div>
怎樣才能解決這個問題?
感謝哥們,原來'player.TeamId = 5;'是關鍵。我不知道我錯過了這個。 – Komengem 2013-03-23 07:15:12
@ Floradu88爲了檢查播放器是否存在,您正在進行不必要的數據庫訪問。如果您在代碼中檢查ID,您已經知道它存在。你應該更新它,因爲它會導致糟糕的性能實踐。 – 2013-03-23 07:17:04
@FeistyMango這是我的原始代碼,之所以我採取這次旅行到數據庫是因爲'Player'是'UserProfile'的孩子,我需要'UserId'作爲'Player' – Komengem 2013-03-23 07:19:02