实体框架支持使用 -1 表示没有关系的旧数据库?

发布于 2024-11-09 00:41:53 字数 543 浏览 0 评论 0原文

我有一个类似于以下示例的数据库:

Customer

CustomerID int
名称...

文档

DocumentID int
客户 ID 整数
DocumentDate...

事情是这样的。并非所有文档都与客户相关。问题是这个旧数据库没有对 Document->CustomerID 使用 int NULL。相反,它是一个不可为 null 的 int,并且使用存储值 -1 来指示它没有客户。在此数据库之上运行的代码知道 -1 是特殊的,表示没有相关实体。

我的问题是现在我想在它上面构建一个实体模型。有没有办法告诉实体模型文档表上的 CustomerID 字段中的 -1 意味着没有关系?实体框架必须将数据库层的 int 映射到模型层的可空 int,并且每当它从数据库检索记录时,它都必须将 -1 的值映射到 null,并且每当保存时,将 null 保存为 -1。当填充实体集时,也必须考虑到这种映射。

这有道理吗?实体框架似乎可能已经内置在某个地方,但话又说回来,也许没有。

有想法吗?

I have a database like the following example:

Customer

CustomerID int
Name...

Document

DocumentID int
CustomerID int
DocumentDate...

Here's the thing. Not all documents are related to a customer. The problem is that this old database doesn't use an int NULL for Document->CustomerID. Instead, it's a nonnullable int, and it uses a stored value of -1 to indicate that it doesn't have a customer. The code that runs on top of this database knows that -1 is special, and indicates no related entity.

My problem is that now I want to build an entity model on top of it. Is there a way to tell the Entity Model that a -1 in the CustomerID field on the Document table means there is no relationship? Entity Framework would have to map the int at the database layer to a nullable int at the model layer, and whenever it retrieved a record from the db, it would have to map the value of -1 to a null, and whenever it saved, save a null as a -1. And when populating entitysets, this mapping would have to be factored in as well.

Does this make sense? It seems like something that Entity Framework might have built in under the covers somewhere, but then again, maybe not.

Ideas?

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

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

发布评论

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

评论(1

一瞬间的火花 2024-11-16 00:41:53

如果数据库没有建立在这些实体之上的关系(根据您的描述,它不能有),EF 将不会为您创建此类关系。如果您使用 EFv4,您可以 自己定义关系为外键关联。

EF 将像处理任何其他关系一样处理此关系。您必须通过将 FK 设置为 -1 或将导航属性设置为 new Customer { Id = -1 } 来欺骗 EF,并且您必须通过调用 <代码>context.ObjectStateManager.ChangeObjectState。否则,EF 将在持久化 Document 时尝试创建或修改此实体。

单独的问题可以是延迟加载\急切加载。我真的不确定当 FK 包含不存在实体的 Id 时 EF 将如何处理情况。我不知何故担心延迟加载和急切加载都会引发异常。

处理这些问题的最简单方法是在数据库中使用 Id -1 创建特殊的 NullCustomer ,以便您使用与真实数据库记录的关系。

EF 不支持您的场景。

If database doesn't have relation built on top of these entities (which it cannot have based on your description) EF will not create such relation for you. If you are using EFv4 you can define the relation yourselves as Foreign key association.

EF will handle this as any other relationship. You will have to cheat EF by setting FK to -1 or navigation property to new Customer { Id = -1 } and you will have to ensure that this dummy entity has always state set to Unchanged by calling context.ObjectStateManager.ChangeObjectState. Otherwise EF will try to create or modify this entity when persisting Document.

Separate problem can be lazy loading \ eager loading. I'm really not sure how will EF handle situation when FK contains Id of non existing entity. I'm somehow afraid that both lazy and eager loading will throw exception.

The easiest way to handle these problems is creating special NullCustomer in your database with Id -1 so that you use relation with a real database record.

EF has no support for your scenario.

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