1
在1个DB调用中执行此操作的最佳方法是什么?减少数据库调用
if (dbContext.Owners.Any(i => i.Value == dog.OwnerID)) //Check if owner exists in db
{
//Assign the owner's contact number to the dog entry
dbDog.OwnerContactNumber = dbContext.Owners.First(i => i.Value == dog.OwnerID).ContactNumber;
}
我的想法:
Owner owner = dbContext.Owners.FirstOrDefault(i => i.Value == dog.OwnerID); //Get the owner
if (owner)
{
dbDog.OwnerContactNumber = owner.ContactNumber; //Assign the contact number
}
但不得不宣布额外的所有者变量,感觉不好。而且我有一堆这些if语句,所以我将不得不创建一堆额外的不需要的所有者变量。
我该如何做得更好?
完美,谢谢! :) – Pierre