Entity Framework在哪里存储对象之间的关系? [英] Where does Entity Framework store the relationship between objects?

查看:81
本文介绍了Entity Framework在哪里存储对象之间的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**我最近刚开始从事Entity Framework项目,我讨厌它,所以请原谅我不喜欢这项技术,但在我看来这不是一个好方法,因为它会产生太多的SQL调用对于最简单的负载; end Entity Framework rant  **


我修改了一个使用Id字段作为另一个表的主键的表;被称为'Id'的字段现在称为TestSetupID,我添加的新字段现在称为ID,是一个标识字段。


延迟加载(我讨厌的另一件事) EF)停止了对这个属性的工作。


EF生成的SQL如下:


SELECT

    [Extent1]。[Id] AS [Id],

    [Extent1]。[SSTempGround] AS [SSTempGround],

    [Extent1]。[SSTempAmbient] AS [SSTempAmbient],

    [Extent1]。[BleedCall] AS [BleedCall],

    [Extent1]。[BleedIncrement] AS [BleedIncrement],

    [Extent1]。[ShowTesttoYield] AS [ShowTesttoYield],

    [Extent1]。[CountTolerance] AS [CountTolerance],

    [Extent1]。[VolumeReleased] AS [VolumeReleased],

    [Extent1]。[PressureReduced] AS [PressureReduced],

    [Extent1]。[PumpEfficiency] AS [PumpEfficiency]

    FROM [dbo]。[DefaultValueTests] AS [Extent1]

    WHERE [Extent1]。[Id] = @ EntityKeyValue1

- EntityKeyValue1:'1133'(Type = Int32,IsNullable = false)

- 执行时间为12/29 / 2015 1:52:34 PM -06:00

- 在1 ms内完成,结果:SqlDataReader


错误的部分是:


WHERE [Extent1]。[Id] = @ EntityKeyValue1;这应该是WHERE [Extent1]。[TestSetupID] = @ EntityKeyValue1;


如何告诉Entity Framework使用新字段。我在数据库中设置关系如下:



我认为这是正确的;是否有一些地方EF存储此关系缓存到错误的值?


谢谢,


-


漂浮很难;显示其他人真的很难。



解决方案

**我最近刚开始从事Entity Framework项目,我很讨厌它,所以请原谅我不喜欢这项技术,但我认为这不是一个好方法,因为它会产生太多的SQL调用对于最简单的负载;最终实体框架
rant  **


那是因为你不知道如何让它停止这样做,比如断开与数据库的连接并停止foreach循环从每次迭代返回数据库再次读取单个对象的数据。您在
列表中获得1,000个对象,然后循环列表并再次阅读1,100次,因为对象列表未与数据库断开连接。  :) 


有些调整可以减少使用任何ORM生成T-SQL的冗长。


懒惰加载(我讨厌EF的另一件事)停止了这个属性。


我不知道你在说什么。 


如果你正确地做父母和子对象,你没有告诉EF做任何事情,那么EF将照顾父母的人口和外键属性本身。  


在我看来这是正确的;是否有一些地方EF存储这种缓存到错误值的关系?


没有,并且你所拥有的身份在数据库架构级别看起来有点可疑。  &的 <强>


** I recently started working on Entity Framework project, and I hate it, so please forgive me for not liking this technology, but it is not a good approach in my opinion as it makes too many SQL calls for the simplest loads; end Entity Framework rant **

I modified a table that was using the Id field to be the primary key from another table; the field that was called 'Id' is now called TestSetupID, and a new field I added that is now called ID, is an Identity field.

The lazy loading (another thing I hate about EF) stopped working on this property.

The SQL Generated by EF is as follows:

SELECT
    [Extent1].[Id] AS [Id],
    [Extent1].[SSTempGround] AS [SSTempGround],
    [Extent1].[SSTempAmbient] AS [SSTempAmbient],
    [Extent1].[BleedCall] AS [BleedCall],
    [Extent1].[BleedIncrement] AS [BleedIncrement],
    [Extent1].[ShowTesttoYield] AS [ShowTesttoYield],
    [Extent1].[CountTolerance] AS [CountTolerance],
    [Extent1].[VolumeReleased] AS [VolumeReleased],
    [Extent1].[PressureReduced] AS [PressureReduced],
    [Extent1].[PumpEfficiency] AS [PumpEfficiency]
    FROM [dbo].[DefaultValueTests] AS [Extent1]
    WHERE [Extent1].[Id] = @EntityKeyValue1
-- EntityKeyValue1: '1133' (Type = Int32, IsNullable = false)
-- Executing at 12/29/2015 1:52:34 PM -06:00
-- Completed in 1 ms with result: SqlDataReader

The part that is wrong is this:

WHERE [Extent1].[Id] = @EntityKeyValue1; this should now be WHERE [Extent1].[TestSetupID] = @EntityKeyValue1;

How do I tell Entity Framework to use the new field. I have the relationship setup in the database as follows:

It looks to me like this is correct; is there some place EF stores this relationship that is cached to the wrong values?

Thank you,

--

To levitate is hard; showing someone else is really hard.

解决方案

** I recently started working on Entity Framework project, and I hate it, so please forgive me for not liking this technology, but it is not a good approach in my opinion as it makes too many SQL calls for the simplest loads; end Entity Framework rant **

That's because you don't know how to make it stop doing it, like disconnecting from the database and stopping a foreach loop from going back to the database on each iteration to read data for individual object again. You get 1,000 objects in a List,  and then you loop on a List and read 1,100 times again, because the List of objects are not disconnected from the database.  :) 

There are adjustments that can cut down on the verboseness of generating T-SQL with any ORM.

The lazy loading (another thing I hate about EF) stopped working on this property.

I don't know what you are talking about. 

You don't tell EF to do anything if you are doing things correctly if you have the parent and children objects within the parent set correctly, then EF will take care of the population of parent and foreign key properties by itself.  

It looks to me like this is correct; is there some place EF stores this relationship that is cached to the wrong values?

Nope, and the Identity you have looks kind of suspect at the database schema level.  


这篇关于Entity Framework在哪里存储对象之间的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆