Entity Core
을 사용하여 내 Db에서 모델의 단일 인스턴스를 만들려고합니다. 내가 작동하게하는 방법은 Db의 엔트리가 존재하지 않는다면 그것을 만드는 것입니다. 작업을 수행하는 게터 메소드를 작성하는 대신 Entity Core
이 작업 할 빈 항목을 생성하고 다시 저장하도록 할 수 있습니까?존재하지 않는 경우 항목 만들기
FirstOrDefault
을 사용해 보았습니다. 여기
using (var context = new SearchTermInformationContext())
{
// this very first line should return me the single instance or create one if it doesnt exist.
var searchTermInfo = context.SearchTermInformation.FirstOrDefault();
searchTermInfo.num_search_terms += numSearchTerms;
searchTermInfo.offset = offset;
searchTermInfo.last_updated += (DateTime.UtcNow.Ticks/TimeSpan.TicksPerMillisecond).ToString();
await context.SaveChangesAsync(ct);
}
[Entity Framework의 가능한 복제본] 업데이트가없는 경우 추가하십시오. (https://stackoverflow.com/questions/31162576/entity-framework-add-if-not-exist-without-update) –
첨부 된 예제조차도 얻을 수 있습니다. –