多个数据库,其模型稍有变化.如何在运行时允许EF与不同的数据库结构一起使用? [英] Multiple databases with slightly changing models. How do I allow `EF` to work with different database structures at run-time?

查看:74
本文介绍了多个数据库,其模型稍有变化.如何在运行时允许EF与不同的数据库结构一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用EF6MSSQLOracle.NET4.5,该系统在整个公司(许多部门)中全局使用,以查询属于我们部门的不同数据库,这些数据库大多数具有相同的EF模型,有些数据库是Oracle,有些是Microsoft SQL,有些是开发或uat,有些是日志.

I am working with EF6, MSSQL, Oracle, .NET4.5 on a system that is used globally across company (many departments) to query different databases that belong to our department, that have mostly same EF model, some databases are Oracle and some are Microsoft SQL, some are development or uat, some are logs.

我正在为OracleMSSQL数据库使用不同的EF模型.

I am using different EF models for Oracle and for MSSQL databases.

一个要求是在运行时在数据库之间进行切换,这很容易,

One requirement is to switch between databases at run time, and this is easy,

public AggregatorEntities(string connectionString) : base(connectionString) { }

public AggregatorEntities(string connectionString) : base(connectionString) { }

但是它确实有副作用-许多数据库(dev,uat,dr,log等)与Live是不同步的(模型是通过Live生成的),这会在查询这些数据库时导致错误.

however it does have side effects - many databases (dev, uat, dr, logs,...) are out of sync from what Live is (model is generated from Live), which results in errors when querying those databases.

管理层了解情况,对于在某些特定数据库上工作的开发人员可以更改全局查询系统(允许测试人员和uat查询数据)而言,它们是可以的.但是,他们希望所做的更改需要花费最少的时间来完成,因为这是涉及数据库更改的每个项目的额外费用.我基本上需要构建一个可以处理所有问题"的弹性系统,以便在EF中更改数据库时可以做一些事情来适应特定的数据库.

Management knows about situation and they are ok for devs that work on some specific database to do changes to global querying system that would allow testers and uat to query the data. However they want changes they have to do to take minimum time to do this - as it is additional cost to each project that involves database changes. I will basically need to build a 'can handle all' resilient system, that when one changes database in EF will do something to accommodate to specific database.

有不同的故障情况:
1.表上的列名称相同,但实体中的类型不同
2.表上没有列,但EF
中的实体上有一列 3.表格上不在EF
上的其他列 4.数据库中不属于EF模型的其他表
5.数据库中没有表,但EF模型中有实体.

There are different failure scenarios:
1. Name of column on table is the same but Type is different in entity
2. No column on table but there is one on entity in EF
3. Additional columns on table that are not on EF
4. Additional tables in database that are not in EF model
5. No table in database but there is entity in EF model.

我已经做了一些思考,这个问题涉及面很广,可能由于相同的原因而被关闭.但是,我不确定是否值得将问题分为每种情况,因为这取决于答案.我的理解方式是,如果单个答案可以回答所有问题,则无需拆分,但是,如果每种情况都有不同的治愈",则应该仅针对该部分拆分问题,而没有答案,则不知道....(第22章) ).

I have done some thinking and this question is broad and might get closed for same reason. However I am not sure if it is worth splitting the question into each scenario, as it depends on the answer. The way I understand if single answer can answer all points then no need to split, however if each situation has different 'cure' then question should be split for that part only, but without answer no way to know.... (catch 22).

我看到ATM的唯一选择是为每个镜像数据库生成它自己的模型,但是最后我得到了50多个模型.

Only option I see ATM is to generate it's own model for each mirroring database, but then I end up with 50+ models.

如何允许EF在运行时使用不同的数据库结构?

How do I allow EF to work with different database structures at run-time?

推荐答案

现在正式不能为但是仍然可以实现在具有相似结构的不同数据库之间切换的最终结果(对于那些没有道德的人). 可以使用带有删除列的部分.

However end result of being able to switch between different databases with similar structures still can be achieved (for those without morals). Part with removing columns can used.

解决方案是使用包含所有表和所有列(在任何数据库中都认为像所有逻辑OR)的数据库生成的包含所有内容的EF模型.然后可以删除特定于运行时所查询环境的,具有来自所有数据库环境的所有属性的所有实体的模型

Solution is to have all inclusive EF model that is generated from database that has all the tables and all the columns (that are in any database think like logical OR of everything). Then model with all entities that have all properties from all db environments can be removed specific to environment that is queried at runtime in mechanism described here. This does not cover cases where type of column changes.

希望这可以为您节省一些时间,因为距我的工作已经花费了2周的时间...

Hope this saves you some time as it took 2 weeks from mine...

这篇关于多个数据库,其模型稍有变化.如何在运行时允许EF与不同的数据库结构一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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