During developing an application with LINQ to SQL, I found that when a stored procedure returns multiple distinct rows with same primary key then LINQ will make the complete list object same. For example, If I have the following table,
在使用LINQ to SQL开发应用程序期间,我发现当存储过程返回具有相同主键的多个不同行时,LINQ将使完整列表对象相同。例如,如果我有下表,
ID Name Salary
-- ---- -----
1 A 20
2 B 200
3 C 30
4 D 520
and my stored procedure returns all rows except with same primary key,
并且我的存储过程返回除了相同主键之外的所有行,
ID Name Salary
-- ---- -----
1 A 20
1 B 200
1 C 30
1 D 520
then linq will bind these rows as,
然后linq将这些行绑定为,
ID Name Salary
-- ---- -----
1 A 20
1 A 20
1 A 20
1 A 20
1 个解决方案
#1
3
It is not a bug it is a feature. Both entity framework and Linq-to-Sql demands uniquely identified entity and entity with each unique key can be loaded only once so when you return multiple records representing same entity type with same key only first record is materialized into entity and this entity is used for representation of all other records with the same key (so it will not only return the same data but the same reference to the entity). It is called identity map and it is key feature of ORM tools.
它不是一个特色的bug。实体框架和Linq-to-Sql都要求唯一标识的实体和实体,每个唯一的密钥只能加载一次,因此当您返回表示具有相同密钥的相同实体类型的多个记录时,只有第一个记录被实现为实体,并且该实体用于使用相同密钥表示所有其他记录(因此它不仅会返回相同的数据,而且会返回与实体相同的引用)。它被称为身份地图,它是ORM工具的关键特征。
#1
3
It is not a bug it is a feature. Both entity framework and Linq-to-Sql demands uniquely identified entity and entity with each unique key can be loaded only once so when you return multiple records representing same entity type with same key only first record is materialized into entity and this entity is used for representation of all other records with the same key (so it will not only return the same data but the same reference to the entity). It is called identity map and it is key feature of ORM tools.
它不是一个特色的bug。实体框架和Linq-to-Sql都要求唯一标识的实体和实体,每个唯一的密钥只能加载一次,因此当您返回表示具有相同密钥的相同实体类型的多个记录时,只有第一个记录被实现为实体,并且该实体用于使用相同密钥表示所有其他记录(因此它不仅会返回相同的数据,而且会返回与实体相同的引用)。它被称为身份地图,它是ORM工具的关键特征。