У меня есть следующая модель домена:
User
{
int Id;
}
City
{
int Id;
}
UserCity
{
int UserId,
int CityId,
dateTime StartDate
}
В функции, где мне нужно присоединить пользователя к городу, у меня работает следующий код:
UserCity uc = new UserCity();
//This is a db hit
uc.User = MyEntityFrameworkDBContext.User.FirstOrDefault(u => u.ID == currentUserId);
//this is a db hit
uc.City = MyEntityFrameworkDBContext.City.FirstOrDefault(c => c.ID == currentCityId);
uc.StartDate = userCityStartDate;
//this is a db hit
MyEntityFrameworkDBContext.SaveChanges();
Можно ли как-нибудь создать отношения с помощью всего одного попадания в БД? На самом деле первые два попадания в дб не требуются.