エンティティフレームワークのモデルコレクションからアイテムを取得しようとしているときにこのエラーが発生します。私は次のエラーを取得する:エンティティをアタッチする際にエラーが発生しました
Attaching an entity of type '' failed because another entity of the same type already has the same primary key value. This can happen when using the 'Attach' method or setting the state of an entity to 'Unchanged' or 'Modified' if any entities in the graph have conflicting key values. This may be because some entities are new and have not yet received database-generated key values. In this case use the 'Add' method or the 'Added' entity state to track the graph and then set the state of non-new entities to 'Unchanged' or 'Modified' as appropriate
誰かが午前はここ を立ち往生助けることができるしてください私のコードです:
public class Attendees
{
[Key]
public int AttendeesId { get; set; }
public ICollection<Programme> programmes { get; set; }
public ICollection<Presentations> presentations { get; set; }
public ICollection<Abstracts> abstracts { get; set; }
[Required]
public bool IsKeynoteSpeaker { get; set; }
}
public class Programme
{
[Key]
public int ProgrammeId { get; set; }
[Required, MaxLength(256,
ErrorMessage = "The length of the field is more than expected")]
public string Title { get; set; }
public ICollection<Attendees> presenters { get; set; }
public ICollection<Attendees> chairpersons { get; set; }
public ProgrammeType programmeType { get; set; }
public ConferenceDay Daynumber { get; set; }
}
public void RemovePresenterFromProgramme(Programme programme,Attendees attendees)
{
var context = _programmeRepository.GetGenericRepository.GetDbContext as ConferenceDbContext;
context.Entry(programme).State = System.Data.Entity.EntityState.Modified; // get the error here
var attendee = programme.presenters
.Where(c => c.AttendeesId == attendees.AttendeesId).SingleOrDefault();
programme.presenters.Remove(attendee);
context.SaveChanges();
}
お返事ありがとうございました。しかし、同じエラーが添付コードにスローされた変更を適用しました。 –
@ user3314516、どこから '(プログラムプログラム)'がロードされたのですか?コンテキストから 'programme'が読み込まれた場合、それを添付しないでください。そうでないと、コンテキスト外で作成された場合は、上記のコードが有効になります。 –