实体框架仅返回一个值,但列表大小正确

发布于 2024-10-22 06:31:54 字数 311 浏览 2 评论 0原文

实体框架仅返回一个值,但列表大小正确

我有一个没有主 ID 的表,我需要获取或选择其中的所有值。

我看到的是,当我使用 linq 进行选择时,对象的数量是正确的,但它一遍又一遍地是第一行。

我只是在做这样的事情

List<MyValueType> valuesInDB = myDb.MyValueTypes.ToList();

问题是我可能会得到数千行(这是正确的),但这些行都具有相同的确切数据。

我正在使用 VS 2010 并使用向导来创建我的 EF 对象。

Entity framework returning only one value but the list size is correct

I have a table that does not have primary id and I need to get or select all the values in it.

What I see is when I do the selection with linq the number of objects is correct but it is the first row over and over.

I am simply doing something like this

List<MyValueType> valuesInDB = myDb.MyValueTypes.ToList();

Problem is I may get thousands of rows (which is correct) but the rows all have the same exact data.

I am using VS 2010 and used the wizard to create my EF object.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

山人契 2024-10-29 06:31:54

问题是实体框架无法在没有密钥的情况下与实体一起工作。因此,如果您的表未指定键,实体框架将推断 自己的。 EF 创建的键由所有不可为空的非二进制列组成。

因此,如果您的实体中有单个不可为空的列,并且只有非常小的值集(例如枚举),您将只能“每个值”加载单个实体。原因是上下文和状态管理器的内部实现使用身份映射模式。当从数据库中检索数据记录时,EF 将首先检查实体键,并尝试在其内部存储中查找具有相同键的对象。如果找到一个对象,它将使用该对象而不是检索到的数据记录(尽管数据不同)。如果未找到具有该密钥的对象,则会具体化新对象并将其添加到内部存储中。

这就是身份映射的目的 - 具有给定键的对象应该由每个上下文仅创建一次。身份映射是 ORM 中的核心模式。

我也在这个问题中写了有关身份映射的文章。

The problem is that entity framework is not able to work with entity without a key. So if your table doesn't specify a key, entity framework will infer its own. The key created by EF is composed of all non-nullable non-binary columns.

So if you for example have single non-nullable column in your entity which have only very small set of values (like enum) you will be able to load only single entity "per value". The reason is an inner implementation of the context and the state manager which uses Identity map pattern. When data record is retrieved from database, EF will first check an entity key and tries to find an object with the same key in its internal storage. If an object is found it will use that object instead of data record retrieved (despite of different data). If an object with the key is not found a new object is materialized and added to internal storage.

That is the purpose of Identity map - object with given key should be created only once by each context. Identity map is core pattern in ORM.

I wrote about Identity map also in this question.

も星光 2024-10-29 06:31:54

我建议在 EDM 的 Designer.cs 文件中搜索“警告”一词。它可能会告诉您实体框架是否对您的表有任何问题。

在没有表格设计的情况下,我真的无法发表太多评论。我尝试复制您的问题,但未能成功。这是我所做的:

  1. 创建一个没有主键的表,但它在 ID 列上有一个唯一键。实体框架能够推断主键,当我获取数据时,我不仅获得了正确的行数,而且还获得了这些行中的正确数据。
  2. 创建一个没有主键和唯一键的表。而且也没有名为 ID 的列。实体框架在生成的 EDM 中排除了此表。因此,我根本无法查询该表。这在 EDM 设计器文件中显示为警告。

如果你能分享你的表的创建脚本那就更好了。

I would suggest searching for the word "Warning" in your EDM's designer.cs file. It might tell you if Entity Framework is having any issues with your table.

I really can't comment much in the absence of the table design. I tried replicating your problem but wasn't able to do so. Here is what I did:

  1. Created a table with no primary key but it had a unique key on an ID column. Entity Framework was able to infer a primary key and when I fetched the data, I not only got the correct number of rows but also the corrects data in those rows.
  2. Created a table with no primary key and no unique key. Also there was no column called ID. Entity Framework excluded this table in the EDM that was generated. Consequently I wasn't able to query this table at all.This was displayed as a warning in the EDM designer file.

It would be better if you can share the create script for your table.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文