结合代码优先和单一模型中的数据库优先? [英] Combine Code First & Database First In Single Model?
问题描述
有没有办法在同一上下文中结合代码优先和数据库优先?在编辑 EDMX 文件时,我们遇到了大量的开发时性能问题(保存需要 1.5 分钟).我已将我们的非插入/更新/删除 UDF/存储过程移动到一些自定义 T4 模板,这些模板会自动生成模型优先代码,但我似乎无法在 EDMX 时调用 OnModelCreating
参与其中.
Is there a way to combine code-first and database-first in the same context? We are running into massive development-time performance problems when editing the EDMX file (it takes 1.5 minutes to save). I've moved our non-insert/update/delete UDFs/stored procs to some custom T4 templates that automatically generate model-first code, but I can't seem to get OnModelCreating
to be called when EDMX is involved.
我们考虑过的其他事情,但由于某种原因不起作用:
Other things we've considered, but won't work for one reason or another:
我们不能(合理地)将我们的代码分离到多个上下文中,因为我们的实体关系中有很多重叠.似乎也有不少走这条路的人后悔了.
We can't (reasonably) separate our code to multiple contexts as there is a lot of overlap in our entity relationships. It also seems like quite a people who have gone this route regret it.
我们尝试了 2 个不同的上下文,但是实体和实体之间有很多连接.UDF.这可能是我们最后的希望,但我真的很想避免它.
We tried having 2 different contexts, but there are quite a few joins between Entities & UDFs. This may be our last hope, but I'd REALLY like to avoid it.
我们无法切换到 Dapper,因为很遗憾我们大量使用了 IQueryable
.
We can't switch to Dapper since we have unfortunately made heavy use of IQueryable
.
我们尝试完全采用 Code-First,但我们在 EDMX 中使用的某些功能不受支持(主要与插入/更新/删除存储过程映射相关).
We tried to go completely to Code-First, but there are features that we are using in EDMX that aren't supported (mostly related to insert/update/delete stored procedure mapping).
推荐答案
感谢大家的深思熟虑和彻底的答案.
Thank you to everyone for the well thought out and thorough answers.
许多其他答案假设 EF Code-First 中的存储过程映射工作相同,但事实并非如此.我对此有点模糊,因为我查看它已经有大约 6 个月了,但我相信从 EF 6.3 开始,代码首先存储过程要求您将每一列从您的实体传递到您的插入/更新存储过程,并且您仅将键列传递给您的删除过程.没有选项可以选择可以传递的列.我们需要维护谁删除了一条记录,因此除了一个简单的键之外,我们还必须传递一些额外的信息.
Many of these other answers assume that the stored procedure mappings in EF Code-First work the same, but they do not. I'm a bit fuzzy on this as it's been about 6 months since I looked at it, but I believe as of EF 6.3 code first stored procedures require that you pass every column from your entity to your insert/update stored procedure and that you only pass the key column(s) to your delete procedure. There isn't an option to pick and choose which columns you can pass. We have a requirement to maintain who deleted a record so we have to pass some additional information besides just a simple key.
话虽如此,我最终做的是使用 T4 模板从数据库中自动生成我的 EDMX/Context/Model 文件(以及一些额外的元数据).这将我们的开发者体验时间从 1.5 分钟缩短到了大约 5 秒.
That being said, what I ended up doing was using a T4 template to automatically generate my EDMX/Context/Model files from the database (with some additional meta-data). This took our developer time experience down from 1.5 minutes to about 5 seconds.
我希望 EF 存储过程映射将得到改进,以实现对 EDMX 的模仿,然后我可以代码生成 Code-First 映射并完全删除 EDMX 生成.
My hope is EF stored procedure mappings will be improved to achieve parody with EDMX and I can then just code-generate the Code-First mappings and remove the EDMX generation completely.
这篇关于结合代码优先和单一模型中的数据库优先?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!