Handling enum changes in Entity Framework 5

拟墨画扇 提交于 2019-12-08 19:38:18

问题


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.

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.

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?


回答1:


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/topic/c-sharp/answers/271483-q-why-casting-enum#post1086722

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.

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



来源:https://stackoverflow.com/questions/11942078/handling-enum-changes-in-entity-framework-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!