简体   繁体   English

如何通过 EF Core 支持获取和插入操作中的动态列?

[英]How to support dynamic columns in get and insert operation via EF Core?

We have a configuration screen where fields can be defined and config is stored in a config table.我们有一个配置屏幕,可以在其中定义字段并将配置存储在配置表中。 In earlier version of the product we were calling SP to check for the columns and created them dynamically in table.在产品的早期版本中,我们调用 SP 来检查列并在表中动态创建它们。

While calling get API, the stored procedure responsible was returning all columns and same was displayed on the blotter.调用 get API 时,存储过程负责返回所有列,并且同样显示在吸墨纸上。

Now we are looking ahead to migrate to EF Core and we are using EF Core to store config information.现在我们期待迁移到 EF Core,并且我们正在使用 EF Core 来存储配置信息。

Here comes the Questions:问题来了:

  • We write stored procedure and pass meta info, which is then used to create columns directly in table, as is current scenario.我们编写存储过程并传递元信息,然后将其用于直接在表中创建列,就像当前场景一样。 Is there any other option in EF Core? EF Core 中还有其他选项吗?
  • Can we somehow get all the columns(dynamically created) using EF Core without using Stored Procedure.我们能否在不使用存储过程的情况下使用 EF Core 以某种方式获取所有列(动态创建)。 We do not want columns to be available in intellisense as no ops is performed on those columns.我们不希望列在智能感知中可用,因为没有对这些列执行任何操作。 They have business use case, so required in return.他们有业务用例,因此需要作为回报。
  • If no to above, then if we write SP, how can same be achieved in EF Core, without creating any Model for the returned object.如果上面没有,那么如果我们写SP,如何在EF Core中实现同样的效果,而不为返回的object创建任何Model。
  • How to perform insert operation on said table from EF Core.如何从 EF Core 对所述表执行插入操作。

Please note one of the main reason to migrate to EF Core is to support multiple DB Server Types(MS SQL Server, MySQL mainly).请注意,迁移到 EF Core 的主要原因之一是支持多种数据库服务器类型(主要是 MS SQL 服务器,MySQL)。 Is there a common way to manage a single version of SP(Routines) across server types.是否有一种通用方法可以跨服务器类型管理单个版本的 SP(例程)。

Just to clarify: EF is Entity Framework and SP is Stored Procedure.澄清一下:EF 是实体框架,SP 是存储过程。

One way to support customer defined properties would be to add an extra table rather than changing the database schema.支持客户定义属性的一种方法是添加一个额外的表,而不是更改数据库模式。 You could for example have a pair of tables like例如,您可以有一对表,例如

PropertyTypeTable属性类型表

Id ID Name姓名 Type类型
1 1 Age年龄 int32整数32

AdditionalPropertiesTable附加属性表

Id ID ReferenceId参考编号 TypeId类型 ID Value价值
1 1 123 123 1 1 42 42

That way you could add additional properties to some table at runtime without actually changing the database schema.这样,您可以在运行时向某个表添加其他属性,而无需实际更改数据库架构。 If the customer wants to add a new column he/she would just add a new row in the propertyTypeTable, and add corresponding rows to the AdditionalPropertiesTable whenever creating a new entry in the table.如果客户想要添加一个新列,他/她只需在 propertyTypeTable 中添加一个新行,并且每当在表中创建新条目时将相应的行添加到 AdditionalPropertiesTable。 Any type of UI would need to be updated to accommodate arbitrary types.任何类型的 UI 都需要更新以适应任意类型。 A potential downside with such a solution would be that type validation need to be done in the application layer rather than the database.这种解决方案的一个潜在缺点是需要在应用程序层而不是数据库中进行类型验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM