简体   繁体   English

如何根据现有数据库验证实体框架 CodeFirst 映射?

[英]How to validate Entity Framework CodeFirst mappings against existing database?

When DbContext is instantiated it validates conceptual model only.DbContext被实例化时,它仅验证概念模型。
But when first query is executed context performs validation against Db.但是当执行第一个查询时,上下文会针对 Db 执行验证。

Is it possible to check all the mappings against database without issuing queries?是否可以在不发出查询的情况下检查所有与数据库的映射?
I'd like something like context.ValidateAgainstDb(connection)我想要像context.ValidateAgainstDb(connection)这样的东西

Now we do such integration testing in the following way:现在我们通过以下方式进行这样的集成测试:

We use AutoFixture to generate 'trash' entities.我们使用 AutoFixture 生成“垃圾”实体。 After that we switch constraints off and add these entities via DbSet calling SaveChanges() .之后,我们关闭约束并通过DbSet调用SaveChanges()添加这些实体。

In principle, this process can be to completely automated.原则上,这个过程可以完全自动化。
But before reinventing the wheel I'd like to know whether ready solution of the problem exists.但在重新发明轮子之前,我想知道是否存在现成的问题解决方案。

DbMigrator.GetPendingMigrations().Any() DbMigrator.GetPendingMigrations().Any()

EDIT编辑
If you are looking for a check without any migration, I think there is not.如果您正在寻找没有任何迁移的支票,我认为没有。 Thinking about how EF migration works (__MigrationHistory contains the POCO model) I think that EF does not have a way to know if the DBMS model is ok for the POCO model.考虑 EF 迁移是如何工作的(__MigrationHistory 包含 POCO 模型)我认为 EF 没有办法知道 DBMS 模型是否适合 POCO 模型。

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

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