将实体框架实体转换为“错误".方向 [英] Casting Entity Framework Entities in the "Wrong" Direction

查看:119
本文介绍了将实体框架实体转换为“错误".方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Entity Framework,它具有一个继承结构,其中包含一个基本Entity(我们将其称为Customer)和一个派生的Entity,我们将其称为AccountCustomer.区别在于,AccountCustomer具有额外的详细信息(例如付款条件等),这些额外的详细信息(例如付款条件等)存储在数据库的单独表中,因此在Entity中具有额外的属性.

I am using the Entity Framework and have an inheritance structure with a base Entity (let's call it Customer) and a derived Entity, let's call it AccountCustomer. The difference is that an AccountCustomer has extra details (such as payment terms etc.) stored in a separate table in the database and therefore extra properties in the Entity.

我想允许用户提升"特定的客户成为AccountCustomer.我需要保留相同的主键(用户可以看到并用作客户参考的组合键).

I want to allow users to 'promote' a specific Customer to be an AccountCustomer. I need to keep the same primary key (a composite key visible to users and used as the customer reference).

此刻,我的感觉是,调用存储过程在Accounts表中创建附加记录是唯一的方法,但是到目前为止,我们还没有绕过Entity Framework,因此如果可能的话,希望避免使用此技术.

At the moment my feeling is that calling a stored procedure to create the additional record in the Accounts table is the only way to go, but up to now we have not bypassed the Entity Framework so would prefer to avoid this technique if possible.

有人有任何以实体框架为重点的解决方案吗?

Has anybody any Entity Framework focussed solutions?

推荐答案

这是请不要这样做"方案之一.

This is one of those "Please don't do this" scenarios.

您是严格按照表而不是面向对象的方式来考虑这一问题的.

You are thinking about this strictly in terms of tables, instead of in object-oriented terms.

特定客户是特定客户.他永远不会改变的事情.现在,他的状态可能会更改,或者他可能会获得其他AccountProperties,但他从未从一种事物(客户)过渡到另一种事物(AccountCustomer).从概念上讲,它根本没有任何意义(一个普通的水果不会变成一个苹果,对吗?不!它从具有一个状态的苹果开始,并最终以一个具有新状态的苹果结束),并且肯定是在.NET面向对象编程中是不可能的……这将使得在像EF这样的ORM中不可能实现.

A particular customer is a particular customer. The kind of thing he is never changes. Now, his Status may change, or he may acquire additional AccountProperties, but he never transitions from being one kind of thing (Customer) to another kind of thing (AccountCustomer). It simply doesn't make sense conceptually (a generic fruit doesn't morph into an apple, does it? no! it starts as an apple with one status, and ends up as an apple with a new status), and it certainly is impossible in .NET object-oriented programming ... which would make it impossible in an ORM like EF.

因此,请考虑一种将其概念化的明智方法,这将导致一种以面向对象的方式表达这一观点的明智方法,这将导致一种明智的EF解决方案.

So please think about a sensible way to conceptualize this, which will lead to a sensible way to express this in object-oriented terms, which will lead to a sensible EF solution.

这篇关于将实体框架实体转换为“错误".方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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