迁移后,EF核心更新因可枚举的可空类型更改而失败

时间:2019-02-27 09:36:31

标签: c# asp.net-core .net-core entity-framework-core ef-core-2.2

我使用的是EF core 2.2.1,更新到最新迁移时遇到以下错误:

  

System.Data.SqlClient.SqlException(0x80131904):无法将值NULL插入表的“ CountryCode”列中   'context.dbo.User';列不允许为空。更新   失败。该声明已终止。

该消息是明确的,并且由于实体的更改,因此在迁移之前是:

def my_view(request):
    context['list_no'] = Visitor.objects.filter('Chat' in list(connecting_medium.values_list('connection',flat=True)))
        #--------------------rest-of-the-code----------------------------#

然后将CountryCode的类型更改为枚举(CountryCode)

public class RegistrationUser : IHaveId, ISearchable
{
    public int Id { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int? CountryCode { get; set; }
    // other properties
}

即使我尝试为枚举设置默认值(仍然与上面相同的错误),实体配置也没有更改:

public class RegistrationUser : IHaveId, ISearchable
{
    public int Id { get; set; }
    public string Email { get; set; }
    public string Password { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public CountryCode CountryCode { get; set; }
    // other properties
}

已添加迁移,但是在Package Manager控制台中使用此命令进行的相关更新失败

  

dotnet ef数据库更新

问题是如何正确更改此类型,在生成的表中具有不为空的具有默认值的列(在EF核心EntityTypeConfiguration中配置的列)

0 个答案:

没有答案