简体   繁体   English

如何在Entity Framework中实施新的数据完整性规则

[英]How to enforce new data integrity rules in Entity Framework

I've got a table that has had a few new columns added, and they allow nulls to accurately accommodate the existing data. 我有一个表添加了一些新列,它们允许空值准确地容纳现有数据。 At the time those records were created the new columns did not exist, so a null accurately represents the state of the database at that time. 在创建这些记录时,新列不存在,因此null准确表示当时数据库的状态。 For all new data I don't want to allow nulls. 对于所有新数据,我不想允许空值。 What is the best practice to manage that? 管理它的最佳做法是什么?

If I use .IsRequired() in the EntityTypeConfiguration class it will try to alter the table to make the column non-nullable and fail when it encounters the historic nulls. 如果我在EntityTypeConfiguration类中使用.IsRequired() ,它将尝试更改表以使列不可为空并在遇到历史空值时失败。 I still want to be able to read those old records, I just don't want to write any new records without the new column. 我仍然希望能够读取那些旧记录,我只是不想在没有新列的情况下编写任何新记录。

I want to do this as close to the DB as possible, so I've put some validation code in the setter. 我想尽可能靠近数据库这样做,所以我在setter中放了一些验证代码。

    private string _CountryCode;
    public string CountryCode
    {
        get
        {
            return _CountryCode;
        }
        set
        {
            if (string.IsNullOrEmpty(value))
                throw new ArgumentException("Country Code Required");
            _CountryCode = value;
        }
    }

This does work, but I have a nagging feeling there is a cleaner solution out there. 这确实有效,但我有一种唠叨的感觉,那里有一个更清洁的解决方案。

Does the answer to this question " Default value for Required fields in Entity Framework migrations? " help you? 这个问题的答案“ 实体框架迁移中必填字段的默认值吗? ”对您有帮助吗?

You have to define a defaultValueSql so all the NULL values can become required. 您必须定义defaultValueSql,以便可以成为所有NULL值。

The proper approach would be using IsRequired() because for EF, it either is Required, or it isn't. 正确的方法是使用IsRequired()因为对于EF,它既可以是必需的,也可以不是。 If it somehow is acceptable not to be there, it is exactly that: not Required. 如果以某种方式接受不存在,那就是:不是必需的。

The way to handle it for me would be to migrate previous data, but if you decide not to do that, your solution seems a good approach to handle it, however, your current setter looks really weird to me. 为我处理它的方法是迁移以前的数据,但如果你决定不这样做,你的解决方案似乎是处理它的好方法,但是,你当前的setter对我来说看起来很奇怪。 For example, if I instantiate the class, and do myObject.CountryCode = "AA"; 例如,如果我实例化该类,并执行myObject.CountryCode = "AA"; , I'd get an exception because the current value, before my set, is null or blank. ,我会得到一个例外,因为我的集合之前的当前值为null或空白。 Sounds erroneous. 听起来很错误。 Maybe you should test value instead? 也许你应该测试value呢?

On a side note, most of the corporate systems I've seen using EF would prevent EF from automatically altering the database. 另外,我见过使用EF的大多数公司系统都会阻止EF自动更改数据库。 It's common (but not universal) practice to use Database.SetInitializer<YourContextClass>(null); 使用Database.SetInitializer<YourContextClass>(null);是常见的(但不是通用的)练习Database.SetInitializer<YourContextClass>(null); in your DbContext to prevent it from doing so automatically, in your production environment. 在您的DbContext以防止它在您的生产环境中自动执行此操作。 You still can call the methods to force an update to the database structure under certain circumstances, or use dacpacs generated by database projects. 在某些情况下,您仍然可以调用方法强制更新数据库结构,或者使用数据库项目生成的dacpac。

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

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