Entity framework EF 6 IsRequired()允许空字符串

Entity framework EF 6 IsRequired()允许空字符串,entity-framework,entity-framework-6,Entity Framework,Entity Framework 6,在过去使用EF5和EF4版本的项目中,如果属性为null或空字符串,IsRequired()fluent API方法将抛出DbEntityValidationException。在我当前的项目UtilityNGEF6中,当string属性为空时,不会引发DBEntityValidationException 实体: public class Application : BaseEntity { public string Name { get; set; } // naviga

在过去使用EF5和EF4版本的项目中,如果属性为null或空字符串,IsRequired()fluent API方法将抛出DbEntityValidationException。在我当前的项目UtilityNGEF6中,当string属性为空时,不会引发DBEntityValidationException

实体:

public class Application : BaseEntity
{
    public string Name { get; set; }

    // navigation properties
    public IList<Role> Roles { get; set; }
}
internal class ApplicationMapping : EntityTypeConfiguration<Application>
{
    public ApplicationMapping()
    {
        // table name
        this.ToTable("Applications");

        // properties
        this.Property(t => t.Name)
            .IsRequired()
            .HasMaxLength(100);
    }
}
公共类应用程序:BaseEntity
{
公共字符串名称{get;set;}
//导航属性
公共IList角色{get;set;}
}
配置:

public class Application : BaseEntity
{
    public string Name { get; set; }

    // navigation properties
    public IList<Role> Roles { get; set; }
}
internal class ApplicationMapping : EntityTypeConfiguration<Application>
{
    public ApplicationMapping()
    {
        // table name
        this.ToTable("Applications");

        // properties
        this.Property(t => t.Name)
            .IsRequired()
            .HasMaxLength(100);
    }
}
内部类应用程序映射:EntityTypeConfiguration
{
公共应用程序映射()
{
//表名
本表为可转让的(“申请”);
//性质
this.Property(t=>t.Name)
.IsRequired()
.HasMaxLength(100);
}
}
在大量阅读了MSDN EF文档和堆栈溢出后,我不知道为什么会发生这种情况。EF6中是否添加/修改了约定?

您可能会混淆和


.IsRequired()
标记数据库中的列为
非空
。但是,如果属性为null、包含空字符串(“”)或仅包含空格字符,则
[必需的]
注释将引发验证异常。

现在,您仍然可以使用
[必需的]
属性并具有可配置的
AllowEmptyString

[Required(AllowEmptyStrings = false)]

False是这里的默认值

EF Core 2.1-看起来像是使用[required]将属性标记为required,并使用空字符串值将其保存到DB中,让我们看一下。。。很奇怪

文件说明如下:

//
// Summary:
//     Gets or sets a value that indicates whether an empty string is allowed.
//
// Returns:
//     true if an empty string is allowed; otherwise, false. The default value is false.
public bool AllowEmptyStrings { get; set; }

您是否已将您的
EntityTypeConfiguration
注册到
OnModelCreated
方法中?因此使用
modelBuilder.Configurations.Add(newapplicationmapping())是,我已经验证了实体类型配置在创建模型时已实例化。IsRequired()在Name属性为null时抛出DBEntityValidationException,但在Name=string时抛出的不是。Empty这是一个很好的建议,fluent api只是在需要时不为null。可能之前还有其他检查筛选空字符串。我刚刚验证了属性上的[Required]属性在存储包含的实体时,如果该属性设置为null或“”,则会触发验证异常。这与EF6有关。但这里的冲突是,您在DB中设置了“not null”,并在VS2015中自动生成poco类,属性标记为[Required],然后您认为可以插入String.Empty,但它会崩溃。这是EF poco生成中的一个错误吗?我不知道为什么他们会让它在空字符串上验证失败。将列标记为非NULL不应禁止空字符串。我的字段不为NULL。但我应该能够在其中插入空字符串。当我有EF数据库第一(edmx)它允许空字符串。现在我先改为EF代码,它会自动将[Required]属性放在prop上,并且不允许使用空字符串。我一直在挣扎,直到我看到了你的解释,但它可能会让人困惑(在DB级别,没有类似的概念……它不在乎你的字符串是空的、有一个字符还是1000个字符!)。如果确实需要插入空字符串。AllowEmptyStrings=true是不够的,您还必须将ConvertEmptyStringToFull=false。不幸的是,
[Required]
属性(来自
DataAnnotations
)根据使用上下文具有不同的语义。例如,在JSON.NET模式验证中,这意味着JSON属性必须存在,但它可以具有
null
或空字符串值。在ASP.NET MVC中,它确实尊重
AllowEmptyStrings
,意味着属性必须有一个值(但如果它是一个复杂的属性,就不能有值),EF Core也在做自己的事情。啊。