我正在 MVC 中创建应用程序。当我尝试将数据插入 SQL Server 2008 时,它显示如下错误:
ObjectStateManager 中已存在具有相同键的对象。现有对象处于未更改状态。只有处于添加状态的对象才能再次添加到 ObjectStateManager。
这是什么意思?
Candidate candidate = _repository.GetCandidate(LoggedInCandidate.Id);
candidate.Name = collection["Name"];
candidate.Email = collection["Email"];
candidate.Address = collection["Address"];
candidate.ContactNumber = collection["ContactNumber"];
candidate.MobileNumber = collection["MobileNumber"];
candidate.LicenseNumber = collection["LicenseNumber"];
int candidateId = _repository.AddCandidate(candidate);
string[] languages = collection["Languages"].Split(',');
foreach (string language in languages)
{
if (!string.IsNullOrEmpty(language))
{
CandidateLanguage cl = new CandidateLanguage();
cl.CandidateId = candidateId;
cl.LanguageId = Convert.ToInt32(language);
_repository.AddCandidateLanguage(cl);
}
}
_repository.Save();
}