在处理实体枚举变化框架5 [英] Handling enum changes in Entity Framework 5

查看:178
本文介绍了在处理实体枚举变化框架5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在<一个href="http://stackoverflow.com/questions/11909537/does-the-new-migrations-feature-of-entity-framework-5-fully-support-enum-changes/11941863#11941863">this问题,我发现,枚举变化没有被实体框架的迁移处理。事实上,枚举变化甚至不导致模式改变的错误,这样你就可以随意,没有控制改变枚举。

In this question, I discovered that enum changes are not handled by Entity Framework migrations. In fact, enum changes don't even result in a model changed error, so you can change enums at will with no controls.

这导致不同的int值,如订单的变化或清除,可以有效地使数据库中的数据无效,因为存储的整数的意思是现在的错误枚举的变化。

Enum changes that result in different int values, such as order changes or removals, can effectively render the database data invalid, since the meaning of the stored integer is now wrong.

为了对迁移工作,你必须手动执行自定义的SQL改变的改变枚举值。

In order for Migrations to work, you have to manually execute custom SQL that changes the changed enum values.

现在的问题是,开发人员必须记住要做到这一点,如果有一个监督则有效的数据可能会损坏。

The problem is, the developer has to remember to do this, and if there was an oversight then effective data corruption can occur.

一个人怎么能落实到位检查这个问题?是否有可能,在事件枚举变化,抛出一个模式的转变错误,或者类似这样的东西吗?

How can someone put into place checks against this? Is it possible to, in the event an enum changes, throw a model change error or something like this?

推荐答案

中使用的类似的问题存在枚举在.net中,当您将它们移出到不同的项目为库:

A similar problem with enums exists in .Net when you move them out to a different Project to be used as a library:

http://bytes.com/主题/升C /答案/ 271483-Q-为什么 - 铸造 - 枚举#post1086722

试试看 - 一般来说枚举是出奇的脆弱。答案是始终指定明确的价值,你的枚举,以prevent这两个问题。这允许您仍然利用自己的核心价值(明确的名称,而不是幻数多一点,以类型安全的方法参数),但prevents你悄悄地破坏了一切。

Try it - enums in general are surprisingly brittle. The answer is to always assign an explicit value to your enums, to prevent both problems. This allows you to still leverage their core value (clear names instead of magic numbers and a little more to type safety in method arguments), but prevents you from quietly breaking everything.

您可以通过正则表达式执行这一政策,code审查或提交后钩子。

You can enforce this policy with code reviews or post-commit hooks via a regex.

这篇关于在处理实体枚举变化框架5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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