如何为实体框架实现其他实体属性?

时间:2022-11-27 06:53:27

We have a requirement to allow customising our core product and adding additional fields on a per client basis e.g. People entity some client wants to record their favourite colour etc. As far as I know we can't add properties to EF at runtime as it needs classes defined at startup. Each customer has their own database but we are deploying the same solution to all customers with all additional code. We are then detecting which customer they are and running customer specific services etc.

我们有一个要求允许定制我们的核心产品和每个客户的基础上添加额外的字段如人实体一些客户想要记录他们最喜欢的颜色等等。据我所知我们不能添加属性在运行时EF,因为它需要在启动时定义的类。每个客户都有自己的数据库,但是我们用所有其他代码将相同的解决方案部署到所有客户。然后我们会检测他们是哪个客户,并运行客户特定的服务等等。

Now the last thing I want is to be forking my project or alternatively adding all fields for all clients. This would seem likely to become a nightmare. Also more often than not the extra fields would only be required in a very limited amount of place. Maybe some reports, couple of screens etc.

现在,我最不想做的事情就是放弃我的项目,或者为所有的客户添加所有字段。这似乎将成为一场噩梦。通常,额外的字段只在非常有限的地方需要。可能有一些报告,一些屏幕等等。

I found this article from Jermey Miller http://codebetter.com/jeremymiller/2010/02/16/our-extension-properties-story/ describing how they are adding extension properties and having them go from domain to the web front end.

我在Jermey Miller http://codebetter.com/jeremymiller/2010/02/16/our-exten-proper-story/中找到了这篇文章,描述了它们如何添加扩展属性,并让它们从域到web前端。

Has anyone else implemented anything similar using EF? How did it work out? Are there any blogs/samples that anyone has seen? I am not sure if I am searching for the right thing even if someone could tell me the generic name for what we want to do that would help. I'm guessing it is a problem that comes up for other people.

还有其他人使用EF来实现类似的功能吗?结果如何?有谁看过的博客/样本吗?我不确定我是否在寻找正确的东西,即使有人能告诉我我们想要做的事情的通用名称会有帮助。我猜这是别人遇到的问题。

1 个解决方案

#1


1  

Linked question still requires some forking or implementing all possible extensions in single solution because you are still creating strongly typed extensions upfront (= you know upfront what extensions customer wants). It is not generally extensible solution. If you want generic extensible solution you must leave strongly typed world and describe extensions as data.

链接问题仍然需要在单个解决方案中分叉或实现所有可能的扩展,因为您仍然在前面创建强类型扩展(=您预先知道扩展客户需要什么)。它不是一般可扩展的解决方案。如果您想要通用的可扩展解决方案,您必须离开强类型的世界,并将扩展描述为数据。

You will need to use some metamodel. Your entity classes will contain only properties used by all customers and navigation property to special extension entity (additional table per every extensible entity) where you will be able to put additional properties as name / value pair (you can add other columns like type, validation, etc. if needed).

您将需要使用一些元模型。您的实体类将只包含所有客户使用的属性和导航属性到特殊的扩展实体(每个可扩展实体都有一个附加表),您可以将附加属性作为名称/值对(如果需要,您可以添加其他列,如类型、验证等)。

This will in general moves part of your model from hardcoded scenario to configuration based scenario and your customers will even be allowed to define extensions at runtime (if you implement such feature).

这通常会将模型的一部分从硬编码场景转移到基于配置的场景,甚至允许客户在运行时定义扩展(如果您实现了这种特性)。

#1


1  

Linked question still requires some forking or implementing all possible extensions in single solution because you are still creating strongly typed extensions upfront (= you know upfront what extensions customer wants). It is not generally extensible solution. If you want generic extensible solution you must leave strongly typed world and describe extensions as data.

链接问题仍然需要在单个解决方案中分叉或实现所有可能的扩展,因为您仍然在前面创建强类型扩展(=您预先知道扩展客户需要什么)。它不是一般可扩展的解决方案。如果您想要通用的可扩展解决方案,您必须离开强类型的世界,并将扩展描述为数据。

You will need to use some metamodel. Your entity classes will contain only properties used by all customers and navigation property to special extension entity (additional table per every extensible entity) where you will be able to put additional properties as name / value pair (you can add other columns like type, validation, etc. if needed).

您将需要使用一些元模型。您的实体类将只包含所有客户使用的属性和导航属性到特殊的扩展实体(每个可扩展实体都有一个附加表),您可以将附加属性作为名称/值对(如果需要,您可以添加其他列,如类型、验证等)。

This will in general moves part of your model from hardcoded scenario to configuration based scenario and your customers will even be allowed to define extensions at runtime (if you implement such feature).

这通常会将模型的一部分从硬编码场景转移到基于配置的场景,甚至允许客户在运行时定义扩展(如果您实现了这种特性)。