Почему Платформа Объекта делает столько Распространений в прямом и обратном направлениях к Базе данных?

У Carlos Aguilar Mares был преобразователь онлайн приблизительно для 40 forevers - Преобразователь кода , но я согласился бы, что Отражатель является лучшим ответом.

11
задан user161433 2 October 2009 в 22:48
поделиться

3 ответа

As explained by AlexJ one of the EF designers http://blogs.msdn.com/alexj/archive/2009/06/19/tip-26-how-to-avoid-database-queries-using-stub-entities.aspx

Also this all falls into the realm on "optimisation" which in not often as simple as it seems

Using the the simple approach, SQL will do a read operation to load the FK (question) and cache the result, and then on a seperate command an insert operation which should be using the cached FK result

Using the attached FK method still results in the server doing a read operation for the FK, it just means one less round trip to the SQL Server. So the question becomes - over time is a round trip more expensive than the increased code complexity?

If the application and SQL Server are on the same machine this overhead is very small

Also, if the FK is a clustered index on a large or wide table the IO overhead can be significantly more than if it is a seperate standard index on just the FK value - assuming that the query optimiser is working correctly :-)

11
ответ дан 3 December 2019 в 07:38
поделиться

На самом деле вам не нужно загружать вопрос, чтобы установить отношение. Вместо этого вы можете просто использовать EntityReference

, например,

Answer.QuestionReference = new EntityReference<Question>();
Answer.QuestionReference.EntityKey 
  = new EntityKey("MyContextName.Question", "Id", questionId); 

Я лично использую метод расширения для установки ключей сущностей

public static void SetEntityKey<T>(this EntityReference value, int id)
{
   value.EntityKey = new EntityKey("ContextName." + typeof(T).Name, "Id", id);
}

Так что вместо этого это будет выглядеть так.

 Answer.QuestionReference = new EntityReference<Question>();
 Answer.QuestionReference.SetEntityKey<Question>(questionId); 
8
ответ дан 3 December 2019 в 07:38
поделиться

Это можно сделать, но в .NET 3.5 это очень болезненно. Они сделали это намного проще в .NET 4.0.

-3
ответ дан 3 December 2019 в 07:38
поделиться
Другие вопросы по тегам:

Похожие вопросы: