处理实体框架中的枚举更改5 [英] Handling enum changes in Entity Framework 5

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

问题描述

这个问题,我发现枚举更改不由Entity Framework迁移处理。事实上,枚举更改甚至不会导致模型更改错误,因此您可以随意更改枚举,无任何控件。

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.

为了让Migrations起作用,您必须手动执行更改更改的枚举值的自定义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/topic/c-sharp/answers/271483-q-why-casting-枚举#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.

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

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